Structure

Structure in c language is a user defined datatype that allows you to hold different type of elements.
Each element of a structure is called a member.
It works like a template in C++ and class in Java. You can have different type of elements in it.
It is widely used to store student information, employee information, product information, book information etc.

Defining structure

The struct keyword is used to define structure. Let's see the syntax:
struct structure_name   
 {  
data_type member1;  
data_type member2;  
   .  
   .  
data_type memeberN;  
     };
Let's see the example to define structure for student in c.
struct student  {
int id;  
char name[60];  
float balance;  
  };
Here, struct is the keyword, student is the tag name of structure; id, name and balance are the members or fields of the structure. Let's understand it by the diagram:


Declaring structure variable

We can declare variable for the structure, so that we can access the member of structure easily. There are two ways to declare structure variable:
1. By struct keyword within main() function
2. By declaring variable at the time of defining structure.
1. By struct keyword within main() function
see the example to declare structure variable by struct keyword. It should be declared within the main function.
struct student {
int id;  
char name[60];  
float balance;  
  };  
Now write given code inside the main() function.
struct student s1, s2;
2. By declaring variable at the time of defining structure
struct student {
int id;  
char name[60];  
float balance;  
  } s1,s2;  

Which approach is good

But if no. of variable are not fixed, use 1st approach. It provides you flexibility to declare the structure variable many times.
If no. of variables are fixed, use 2nd approach. It saves your code to declare variable in main() fuction.

Accessing members of structure

There are two ways to access structure members:
1. By . (member or dot operator)
2. By -> (structure pointer operator)
Let's see the code to access the id member of p1 variable by . (member) operator.
p1.id

Structure example

see a simple example of structure in C.
#include <stdio.h>  
struct student {   
int id;    
char name[60];    
 } s1;  //declaring s1 variable for structure  
int main( ) {    
     //store first student information  
s1.id=110;  
strcpy(s1.name, "pramesh gupta");  //copying string into char array  
   //printing first student information  
printf( "student 1 id : %d\n", s1.id);  
printf( "student 1 name : %s\n", s1.name);  
return 0;  
  }  
Output:
student 1 id : 110
student 1 name : pramesh gupta
see another example of structure in C language to store many employees information.
#include <stdio.h>  
struct student {   
int id;    
char name[60];    
float balance;    
  } s1,s2;   //declaring s1 and s2 variables for structure  
int main( ) {  
   //store first student information  
s1.id=110;  
strcpy(s1.name, "pramesh gupta");   //copying string into char array  
s1.balance=35000;  
    //store second student information  
s2.id=112;  
strcpy(s2.name, "ravi ranjan");  
s2.balance=86000;  
   //printing first student information  
printf( "student 1 id : %d\n", s1.id);  
printf( "student1 name : %s\n", s1.name);  
printf( "student 1 balance : %f\n", s1.balance);  
  //printing second student information  
printf( "student 2 id : %d\n", s2.id);  
printf( "student 2 name : %s\n", s2.name);  
printf( "student 2 balance : %f\n", s2.balance);  
return 0;  
  }  
Output:
student 1 id : 110
student 1 name : pramesh gupta
student 1 balance : 35000.000000
student 2 id : 112
student 2 name : ravi ranjan
student 2 salary : 86000.000000