C Structure

In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that an entity has all the information of one type only. It can have different attributes of different data types. For example, an entity Student may have its name (string), roll number (int), marks (float). To store such type of information regarding an entity student, we have the following approaches:

  • Construct individual arrays for storing names, roll numbers, and marks.
  • Use a special data structure to store the collection of different data types.

Let's look at the first approach in detail.

  1. #include<stdio.h>  
  2. void main ()  
  3. {  
  4.   char names[2][10],dummy; // 2-dimensioanal character array names is used to store the names of the students   
  5.   int roll_numbers[2],i;  
  6.   float marks[2];  
  7.   for (i=0;i<3;i++)  
  8.   {  
  9.       
  10.     printf("Enter the name, roll number, and marks of the student %d",i+1);  
  11.     scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);  
  12.     scanf("%c",&dummy); // enter will be stored into dummy character at each iteration  
  13.   }  
  14.   printf("Printing the Student details ...\n");  
  15.   for (i=0;i<3;i++)  
  16.   {  
  17.     printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);  
  18.   }  
  19. }  

Output

Enter the name, roll number, and marks of the student 1Arun 90 91        
Enter the name, roll number, and marks of the student 2Varun 91 56      
Enter the name, roll number, and marks of the student 3Sham 89 69

Printing the Student details...
Arun 90 91.000000                                                                      
Varun 91 56.000000  
Sham 89 69.000000

What is Structure

Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member. Structures ca; simulate the use of classes and templates as it can store various information

The ,struct keyword is used to define the structure. Let's see the syntax to define the structure in c.

  1. struct structure_name   
  2. {  
  3.     data_type member1;  
  4.     data_type member2;  
  5.     .  
  6.     .  
  7.     data_type memeberN;  
  8. };  

Let's see the example to define a structure for an entity employee in c.

  1. struct employee  
  2. {   int id;  
  3.     char name[20];  
  4.     float salary;  
  5. };  

The following image shows the memory allocation of the structure employee that is defined in the above example.

c structure memory allocation

Here, struct is the keyword; employee is the name of the structure; idname, and salary are the members or fields of the structure. Let's understand it by the diagram given below:

c structure

Declaring structure variable

We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable:

  1. By struct keyword within main() function
  2. By declaring a variable at the time of defining the structure.

C Structure example

Let's see a simple example of structure in C language.

  1. #include<stdio.h>  
  2. #include <string.h>    
  3. struct employee      
  4. {   int id;      
  5.     char name[50];      
  6. }e1;  //declaring e1 variable for structure    
  7. int main( )    
  8. {    
  9.    //store first employee information    
  10.    e1.id=101;    
  11.    strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array    
  12.    //printing first employee information    
  13.    printf( "employee 1 id : %d\n", e1.id);    
  14.    printf( "employee 1 name : %s\n", e1.name);    
  15. return 0;  
  16. }    

Output:

employee 1 id : 101
employee 1 name : Sonoo Jaiswal

Let's see another example of the structure in C language to store many employees information.

  1. #include<stdio.h>  
  2. #include <string.h>    
  3. struct employee      
  4. {   int id;      
  5.     char name[50];      
  6.     float salary;      
  7. }e1,e2;  //declaring e1 and e2 variables for structure    
  8. int main( )    
  9. {    
  10.    //store first employee information    
  11.    e1.id=101;    
  12.    strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array    
  13.    e1.salary=56000;    
  14.     
  15.   //store second employee information    
  16.    e2.id=102;    
  17.    strcpy(e2.name, "James Bond");    
  18.    e2.salary=126000;    
  19.      
  20.    //printing first employee information    
  21.    printf( "employee 1 id : %d\n", e1.id);    
  22.    printf( "employee 1 name : %s\n", e1.name);    
  23.    printf( "employee 1 salary : %f\n", e1.salary);    
  24.     
  25.    //printing second employee information    
  26.    printf( "employee 2 id : %d\n", e2.id);    
  27.    printf( "employee 2 name : %s\n", e2.name);    
  28.    printf( "employee 2 salary : %f\n", e2.salary);    
  29.    return 0;    
  30. }    

Output:

employee 1 id : 101
employee 1 name : Sonoo Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000