Array of Structure in C Programming

A structure in C programming language is used to store set of parameters about an object/entity. We sometime want to store multiple such structure variables for hundreds or objects then Array of Structure is used.

Both structure variables and in-built data type gets same treatment in C programming language. C language allows us to create an array of structure variable like we create array of integers or floating point value. The syntax of declaring an array of structure, accessing individual array elements and array indexing is same as any in-built data type array.

Declaration of Structure Array in C

struct Employee
{
    char name[50];
    int age;
    float salary;
}employees[1000];
or
struct Employee employees[1000];

In above declaration, we are declaring an array of 1000 employees where each employee structure contains name, age and salary members. Array employees[0] stores the information of 1st employee, employees[1] stores the information of 2nd employee and so on.

Accessing Structure Fields in Array

We can access individual members of a structure variable as

array_name[index].member_name
For Example
employees[5].age

C Program to Show the Use of Array of Structures

In below program, we are declaring a structure "employee" to store details of an employee like name, age and salary. Then we declare an array of structure employee named "employees" of size 10, to store details of multiple employees.

#include <stdio.h>
#include <conio.h>

struct employee {
    char name[100];
 int age;
 float salary;
};

int main(){
   struct employee employees[10];
   int counter, index, count, totalSalary;
   
   printf("Enter Number of Employees\n");
   scanf("%d", &count);
   
   /* Storing employee detaisl in structure array */
   for(counter=0; counter<count; counter++){ 
       printf("Enter Name, Age and Salary of Employee\n");
       scanf("%s %d %f", &employees[counter].name, 
           &employees[counter].age, &employees[counter].salary);
   }
   
   /* Calculating average salary of an employee */
   for(totalSalary=0, index=0; index<count; index++){
       totalSalary += employees[index].salary;
   }
   
   printf("Average Salary of an Employee is %f\n", 
       (float)totalSalary/count);

   getch();
   return 0;
}

Program Output
Enter Number of Employees
3
Enter Name, Age and Salary of Employee
Jack 30 100
Enter Name, Age and Salary of Employee
Mike 32 200
Enter Name, Age and Salary of Employee
Nick 40 300
Average Salary of an Employee is 200.000000