Array of structure in C Programming Language.

Let us assume that we are going to develope a software for a company y to manage all employees information. Employees has employee number, employee salary, employee name,employee department number,employee job and so on.
To store such kind of information we did use structure . what we can use if you have to store information for more than one employees? The solution for the given problem is array of structure .
we can use structure to store this kind of imformation .However the complexity of the program will increase if there are 10 employees.In this case we have to declare 10 different structure variable and store them one by one.This process will be tough since we have to declare a variable every time to add new employee.To manage and remember the structure variable name is very tricky task. To avoid this C language enables us to declare an array of structure.Array of structure mean collection of all the structures that store the information of differnt entities.
Let understand the array of structure pictorialy from the given figure.
  struct employee
  {
    int empno;
    char ename[5];
   int salary;
  };
 struct employee e[2];
we declared the structure and have created the struct variable e[2]. The array of structure is created i.e e[2].
The array of structure e[2] store the information of two different employees.
Details of first employees will be stored at e[0]the first index position of the array. and the second employees will be at e[1].It is a contiguous allocation of memory for composite types.
The total memory allocation for array of structure from the given fig. is 26 bytes.

Note: Some time the allocation of bytes depends on the system architecture.

Advantage of Array of Structure in C programming.

The advantage of an array of structure in C language is
1.  With the help of an array user can reprsent multiple values with a single array Variable name.
2. The array of structure improves code reusability and readability is also increased.
3.  Multiple structure variables or redudent variables can be stored in single variable.
e.g. No need to remember structure variables like:
struct employee e1, e2, e3;
Instead, use an array of structures like: struct employee e[3];
4. An array of structure contains all the structures variables that store the data of separate entities or structure variables.

  struct employee
  {
    int empno;
    char ename[30];
   char job[30];
   int salary;
   float bonus;
   int deptno;
  };
 struct employee e[2];


Array of Structure Example in C language.

Create array of structure to take input of many employees and display the employees information.

#include<stdio.h>
struct employee
  {
  int empno;
  char name[20];
  float salary;
 };
 int main()
  {
  struct employee e[2];
  for (int i = 0; i < 2; i++)
   {
   printf("\nEnter details of employee %d\n", i + 1);
   printf("\nEnter emp no: ");
   scanf("%d", &e[i].empno);
   printf("Enter name: ");
   scanf("%s", e[i].name);
   printf("\nEnter salary: ");
   scanf("%f", &e[i].salary);
   }
  printf("\nEmployee Information\n");
 printf("Empno\t\tName\t\t\tSalary\n");
  for (int i = 0; i < 2; i++)
 {
   printf("%d\t\t%s\t\t\t%.2f \n", s[i].empno, s[i].name, s[i].salary);
 }
return 0;
}

Output:
Enter details of employee 1:
Enter emp no: 101
Enter name: Ajay
Enter salary:34500
Enter details of employee 2:
Enter emp no: 102
Enter name: Kajol
Enter salary: 34500.
Employee Information
empno Name salary.
101  Ajay  34500
102  Kajo;  34500

Previous Topic:-->> Copy and Compare structurex || Next topic:-->>Array within Structure.