Table of Contents

    Array of Structures in C: Usage and Examples

    Array of Structures in C: Usage and Examples

    C Structure is a collection of different data types ( variables ) which are grouped together. Whereas, an array of structures is nothing but a collection of structures. This is also called a structure array in C.

    This program is used to store and access id, name and percentage for 3 students. Structure array is used in this program to store and display records for many students. You can store n number of students record by declaring structure variable as struct student record[n], where n can be 2000 or 4000 etc.

    Example Program

    #include 
    #include 
     
    struct student 
    {
         int id;
         char name[30];
         float percentage;
    };
     
    int main() 
    {
         int i;
         struct student item[2];
     
         // 1st student's item
         item[0].id=21;
         strcpy(item[0].name, "Rambo");
         item[0].percentage = 86.5;
     
         // 2nd student's item         
         item[1].id=2;
         strcpy(item[1].name, "Azmi");
         item[1].percentage = 90.5;
     
         // 3rd student's item
         item[2].id=3;
         strcpy(item[2].name, "Rahul");
         item[2].percentage = 81.5;
     
         for(i=0; i

    Output

    items of STUDENT : 1
     Id is: 21
     Name is: Rambo
     Percentage is: 86.500000
    
    items of STUDENT : 2
     Id is: 2
     Name is: Azmi
     Percentage is: 90.500000
    
    items of STUDENT : 3
     Id is: 3
     Name is: Rahul
     Percentage is: 81.500000
    
    Press any key to continue . . .

    It is possible to define an array of structures for example if we are maintaining information of all the students in the college and if 100 students are studying in the college. We need to use an array than single variables. We can define an array of structures as shown in the following example:

    structure information 
    { 
    int id_no; 
    char name[20]; 
    char address[20]; 
    char combination[3]; 
    int age; 
    } 
    student[100];

    An array of structures can be assigned initial values just as any other array can. Remember that each element is a structure that must be assigned corresponding initial values as illustrated below.

    Example Program

    #include 
    struct info 
    { 
    int id_no; 
    char name[20]; 
    char address[20]; 
    char sex[7]; 
    int age; 
    };
     
    
    void main(){
    struct info std[100]; 
    int i,n; 
    printf("Enter the number of students\n"); 
    scanf("%d",&n); 
    
    printf("Enter Id_no, name, address, sex age\n"); 
    
    for(i=0; i < n;i++) {
    scanf("%d%s%s%s%d",
    &std[i].id_no, std[i].name, std[i].address, std[i].sex,&std[i].age); 
    }
    printf("\n Student information \n"); 
    
    for (i=0;i

    Output

    Enter the number of students
    2
    Enter Id_no, name, address, sex age
    21 Rambo kolkata male 21
    22 Azmi kolkata male 22
    
     Student information
    Id no: 21 Name: Rambo Address: kolkata sex: male age: 21
    Id no: 22 Name: Azmi Address: kolkata sex: male age: 22
    Press any key to continue . . .