Structures in C Programming Language(Basics)- Declaration and accessing

The array allows us to store multiple data of the same data type under the same name. What if we need to store multiple data of different data type under the same name? No problem, C Programming language allows us to create structures which let us store multiple data belonging to different data types under the same name.

Structures are mostly used for maintaining records like mark sheets with student details like name, ID number, and marks stored under the same name. Another example is for maintaining library records with the name of the book, author, book ID, and subject stored under the same name. This is how data gets stored in a structure:

Structures


Advantages of Structures over Arrays

To begin with, structures can handle multiple data types that are not possible in arrays. Arrays can only handle the same type of data. Consider the example stated above of the students’ details. We can make use of the array in order to store and access the name, ID number, and marks but we lose a particular identity relating the data to only one student. In the case of structures, this is not a problem. We can store and access the data relating to one student without losing the particular identity even with an enormous amount of data.


Declaring a structure

We already know that structure falls under user-defined data types.  So we define a new data type using the keyword “struct“. The syntax of the declaration is given below:

struct structure_name // give any name to your structure
{
datatype1 element1;
datatype2 element2;
datatype3 element3;
};

Now we can define variables that shall contain the elements declared under “struct“. This is shown below:

struct structure_name //Give any name to your structure
{
datatype1 element1;
datatype2 element2;
datatype3 element3;
};
struct structure_name variable_declarations;

Let us consider the student example:

struct student
{
char name[10];
int ID;
float marks;
};
struct student s1,s2,s3;
Note:
  • The declaration of a structure only defines the form of the structure and not allocates memory for storage. The memory is allocated after defining the variables. Eg: s1,s2,s3;
  • The declaration of a structure should be followed by a semicolon(;).
  • If we initialize a structure variable to {0} then all its elements will be initialized with a value 0. Eg: struct student s1={0};
  • Structures are declared before any function or variable is defined. For complex codes, they are put in another file and called in the program as header files using #include.

Accessing Structure elements

There are two ways to access the structure elements –

1) Using Dot(.) operator

In an array, we use the subscript to access the different members of the array. In structures, we use the dot operator (.) also known as the member access operator. This operator is placed between the structure variable name and the structure element that we need to access. For example: if we need to access the name of the first student then we call it in the following format: s1.name

Similarly, for id: s1.id

Printing these values:

Program to store and display the student details using Structure and Dot operator

#include <stdio.h>
int main()
{
    struct student//declaring the structure with name student
    {
        char name[10];//stores the name of the student
        int id;//stores the ID of the student
        float marks;//stores the marks of the student
    };
    
    /*initializing values to all the structure variables*/
    struct student s1={"Ram",101,79.0};
    struct student s2={"Mohan",102,99.0};
    struct student s3={"Rohan",103,55.0};
    struct student s4={0};//all values initialized as 0
    
    /*displaying student details*/
    printf("Details of student1:\n");
    printf(" Name= %s\n ID= %d\n marks= %f\n\n",s1.name,s1.id,s1.marks);
    printf("Details of student2:\n");
    printf(" Name= %s\n ID= %d\n marks= %f\n\n",s2.name,s2.id,s2.marks);
    printf("Details of student3:\n");
    printf(" Name= %s\n ID= %d\n marks= %f\n\n",s3.name,s3.id,s3.marks);
    printf("Details of student4:\n");
    printf(" Name= %s\n ID= %d\n marks= %f\n\n",s4.name,s4.id,s4.marks);
    return 0;
}

Output:-
Details of student1:
 Name= Ram
 ID= 101
 marks= 79.000000

Details of student2:
 Name= Mohan
 ID= 102
 marks= 99.000000

Details of student3:
 Name= Rohan
 ID= 103
 marks= 55.000000

Details of student4:
 Name= 
 ID= 0
 marks= 0.000000

2) Using arrow(->) operator

Another way to access the structure is using the arrow operator ( -> operator). This is mostly used in case of structure pointers or when we pass the address of a structure to a function(call by reference). This operator is used in the same way as the dot operator, i.e., between the structure pointer name and structure element. Here is an example of how we use an arrow operator (This program only discuss about how to access a value of a structure using a pointer but if you have to create a structure based on user value then you have to use memory allocation using malloc or any other related method along with scanf() to read the values for the declared structure pointer):

Program to store and display the student details using Structure and Arrow operator

#include <stdio.h>

/*declaring the structure*/
struct student{
    char name[10];
    int id;
    double marks;
};

void display(struct student *);//declaring the function to display the contents of the structure address passed to the function

int main()
{
    struct student s1={"Ram",101,79.0};
    struct student s2={"Mohan",102,99.0};
    struct student *x;
    x = &s1;//storing the address of the structure variable s1
    
    /*displaying the details of student1 using pointers*/
    printf("The student details are as follows:\n");
    printf(" Name= %s\n ID= %d\n marks= %f\n\n",x->name,x->id,x->marks);
    
    display(&s2);//sending the address of the structure variable s2 to function display()
    
    return 0;
}

/*displaying the contents of the address sent to the function*/
void display(struct student *s)
{
    printf("The student details are as follows:\n");
    printf(" Name= %s\n ID= %d\n marks= %f\n\n",s->name,s->id,s->marks);
}

Output:-
The student details are as follows:
 Name= Ram
 ID= 101
 marks= 79.000000

The student details are as follows:
 Name= Mohan
 ID= 102
 marks= 99.000000

 

Note :- Click here to read more about various features of a structure in C Programming Language


Learning never exhausts the mind.So, do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Share and subscribe.

Keep Coding!! Happy Coding!! :)