About array of structure in C programming

The collection of multiple structures variables where each variable contains information about different entities is called array of structure. We use array of structures to store information about multiple entities of various data types. Array of structures is also called collection of structures.

In this article we will learn about array of structures in C. The advantages of using this type of structure is also discussed here. Before learning array of structure lets see the following program.

// program without array of structure

#include <stdio.h>
struct student_info{
  char name[30];
  int rollNo;
  float cgpa;
};

int main(){
  struct student_info s1,s2,s3;

  printf("Enter name, roll no and CGPA of student 1 : ");
  scanf("%s %d %f",s1.name,&s1.rollNo,&s1.cgpa);

  printf("Enter name, roll no and CGPA of student 2 : ");
  scanf("%s %d %f",s2.name,&s2.rollNo,&s2.cgpa);

  printf("Enter name, roll no and CGPA of student 3 : ");
  scanf("%s %d %f",s3.name,&s3.rollNo,&s3.cgpa);

  printf("\nPrinting students information..........\n");

  printf("%s %d %.2f\n",s1.name,s1.rollNo,s1.cgpa);
  printf("%s %d %.2f\n",s2.name,s2.rollNo,s2.cgpa);
  printf("%s %d %.2f\n",s3.name,s3.rollNo,s3.cgpa);
  return 0;
}

Output of this array of structure program:

array of structure in c, c program using array of structure

Here we have stored data of three students in the structure. Although we have stored the information of only three students here but in real life we have to store data of many students which will make the program very complex if we follow the method which we have done in the above example.

In that case it will be very complex to declare a variable each time. For this C gives us the features to use array of structure to make this task easy. Instead of declaring the different variables we can make a collection of all the structures which store information of different entities.

C program using array of structure

Here we have showed the data of 5 students using an array of structure. Obviously this is the easiest method to store information. You can take more student information here. Let’s see the program bellow;

// c program using array of structures

#include<stdio.h>
#include <string.h>
struct student{
  int rollno;
  char name[10];
};

int main(){
 int c;
 struct student st[5];
 printf("Enter Information of 5 students : \n");
 for(c = 0; c < 5; c++){
    printf("\nEnter Name : ");
    scanf("%s",&st[c].name);
    printf("Enter Roll no : ");
    scanf("%d",&st[c].rollno);

  }
  printf("\nList of students information : \n");
  for(c = 0; c < 5; c++){
    printf("\nRoll no : %d, Name : %s",st[c].rollno,st[c].name);
  }
return 0;
}

Array of structure program output:

Enter Information of 5 students :

Enter Name : Alex
Enter Roll no : 201

Enter Name : John
Enter Roll no : 202

Enter Name : Sara
Enter Roll no : 203

Enter Name : Jeam
Enter Roll no : 204

Enter Name : Lucy
Enter Roll no : 205

List of students information :

Roll no : 201, Name : Alex
Roll no : 202, Name : John
Roll no : 203, Name : Sara
Roll no : 204, Name : Jeam
Roll no : 205, Name : Lucy

Previous pageTypedef keyword in C

Next pageStructure and pointer in C