Array of Structures in C

In C Programming, Structures are useful to group different data types to organize the data in a structural way. And Arrays are used to group the same data type values. In this article, we will show you the Array of Structures in C concept with one practical example.

For example, we are storing employee details such as name, id, age, address, and salary. We usually group them as employee structure with the members mentioned above. We can create the structure variable to access or modify the structure members. A company may have 10 to 100 employee, how about storing the same for 100 employees?

In C Programming, We can easily solve the problem mentioned above by combining two powerful concepts Arrays of Structures in C. We can create the employee structure. Then instead of creating the structure variable, we create the array of a structure variable.

Declaring C Array of Structures at structure Initialization

Let me declare an Array of Structures in C at the initialization of the structure

/* Array of Structures in C Initialization */
struct Employee
{
  int age;
  char name[50];
  int salary;
} Employees[4] = {
                 {25, "Suresh", 25000},
                 {24, "Tutorial", 28000},
                 {22, "Gateway", 35000},
                 {27, "Mike", 20000}
                 };

Here, Employee structure is for storing the employee details such as age, name, and salary. We created the array of structures variable Employees [4] (with size 4) at the declaration time only. We also initialized the values of each structure member for all 4 employees.

From the above C Programming  code,

Employees[0] = {25, "Suresh", 25000}

Employees[1] = {24, "Tutorial", 28000}

Employees[2] = {22, "Gateway", 35000}

Employees[3] = {27, "Mike", 20000}

Declaring C Array of Structures in main() Function

/* Array of Structures in C Initialization */
struct Employee
{
  int age;
  char name[50];
  int salary;
};

Within the main() function, Create the Employee Structure Variable

struct Employee  Employees[4];
Employees[4] = {
                 {25, "Suresh", 25000},
                 {24, "Tutorial", 28000},
                 {22, "Gateway", 35000},
                 {27, "Mike", 20000}
                 };

Array of Structures in C Example

This program for an Array of Structures in C will declare the student structure and displays the information of N number of students.

/* Array of Structures in C example */

#include <stdio.h>

struct Student
{
  char Student_Name[50];
  int C_Marks;
  int DataBase_Marks;
  int CPlus_Marks;
  int English_Marks;
};

int main()
{
  int i;
  struct Student Students[4] = 
                {
                 {"Suresh", 80, 70, 60, 70},
                 {"Tutorial", 85, 82, 65, 68},
                 {"Gateway", 75, 70, 89, 82},
                 {"Mike", 70, 65, 69, 92}
                 };
  
  printf(".....Student Details....");
  for(i=0; i<4; i++)
  {
    printf("\n Student Name = %s", Students[i].Student_Name);
    printf("\n First Year Marks = %d", Students[i].C_Marks);
    printf("\n Second Year Marks = %d", Students[i].DataBase_Marks);
    printf("\n First Year Marks = %d", Students[i].CPlus_Marks);
    printf("\n Second Year Marks = %d", Students[i].English_Marks);
  }    
 
 return 0;
}

Array of Structures in C

Within this Array of Structures in C example, We declared the student structure with Student Name, C Marks, DataBase Marks, C++ Marks, and English Marks members of different data types.

Within the main() function, we created the Array of structures student variable. Next, we initialized the appropriate values to the Structure members

In the Next line, we have For Loop in C Programming Condition inside the for loop. It will control the compiler not to exceed the array limit. The below printf statements will print the values inside the student structure array.

printf("\n Student Name        = %s", Students[i].Student_Name);

printf("\n C Programming Marks = %d", Students[i].C_Marks);

printf("\n Data Base Marks     = %d", Students[i].DataBase_Marks);

printf("\n C++ Marks           = %d", Students[i].CPlus_Marks);

printf("\n English Marks       = %d", Students[i].English_Marks);

Let us explore the Array of Structures in C program in iteration wise

First Iteration

Student Name = Students[i].Student_Name

C Programming Marks = Students[i].C_Marks

Data Base Marks = Students[i].DataBase_Marks

C++ Marks = Students[i].CPlus_Marks

English Marks = Students[i].English_Marks

i = 0 and the condition 0 < 4 is TRUE so

Student Name = Students[0].Student_Name = Suresh

C Programming Marks = Students[0].C_Marks = 80

Data Base Marks = Students[0].DataBase_Marks= 70

C++ Marks = Students[0].CPlus_Marks = 60

English Marks = Students[0].English_Marks = 70

i value incremented by 1 using i++ Incremental Operator. So i becomes 1

Second Iteration of Array of Structures in C

i = 1 and the condition 1 < 4 is TRUE

Student Name = Students[1].Student_Name = Tutorial

C Programming Marks = Students[1].C_Marks = 85

Data Base Marks = Students[1].DataBase_Marks = 82

C++ Marks = Students[1].CPlus_Marks = 65

English Marks = Students[1].English_Marks = 68

i value incremented by 1. So i becomes 2

Third Iteration

i = 2 and the condition 2 < 4 is TRUE

Student Name = Students[2].Student_Name = Gateway

C Programming Marks = Students[2].C_Marks = 75

Data Base Marks = Students[2].DataBase_Marks = 70

C++ Marks = Students[2].CPlus_Marks = 89

English Marks = Students[2].English_Marks = 82

i value incremented by 1. So i becomes 3

Fourth Iteration of the Array of Structures in C

i = 3, and the condition 3 < 4 is TRUE so,

Student Name = Students[0].Student_Name = Mike

C Programming Marks = Students[0].C_Marks = 70

Data Base Marks = Students[0].DataBase_Marks = 65

C++ Marks = Students[0].CPlus_Marks = 69

English Marks = Students[0].English_Marks = 92

i value incremented by 1 using i++ incremental Operator. So, i becomes 4, and the i<4 condition Fails. So, the compiler will exit from the loop.