Open In App

Array of Structures in C

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When dealing with a large set of related data and different data types, organizing and managing it efficiently is crucial. In C programming, the combination of arrays and structures i.e. array of structures provides a powerful tool for managing that. In this article, we discuss the concept of an Array of Structures in C.

What is Array?

The array is a homogeneous collection of elements stored in the continuous memory location. The size of the array is fixed and we can randomly access the elements using their index.

Declaration of Array

array_type array_name[size];

What is Structure?

The structure is one of the user-defined data types in C that can contain elements of different types as its members.

Declaration of a Structure in C

struct structure_name{
memberType memberName;
...
...
};

Array of Structures

An array whose elements are of type structure is called array of structure. It is generally useful when we need multiple structure variables in our program.

Need for Array of Structures

Suppose we have 50 employees and we need to store the data of 50 employees. So for that, we need to define 50 variables of struct Employee type and store the data within that. However, declaring and handling the 50 variables is not an easy task. Let’s imagine a bigger scenario, like 1000 employees.

So, if we declare the variable this way, it’s not possible to handle this.

struct Employee emp1, emp2, emp3, .. . ... . ..  ... emp1000;

For that, we can define an array whose data type will be struct Employee soo that will be easily manageable.

Declaration of Array of Structures

struct structure_name array_name [number_of_elements];

Initialization of Array of Structures

We can initialize the array of structures in the following ways:

struct structure_name array_name [number_of_elements] = {
{element1_value1, element1_value2, ....},
{element2_value1, element2_value2, ....},
......
......
};

The same initialization can also be done as:

struct structure_name array_name [number_of_elements] = {
element1_value1, element1_value2 ....,
element2_value1, element2_value2 .....
};

GNU C compilers supports designated initialization for structures so we can also use this in the initialization of an array of structures.

struct structure_name array_name [number_of_elements] = {
{.element3 = value, .element1 = value, ....},
{.element2 = value, .elementN = value, ....},
......
......
};

Example of Array of Structure in C

C




// C program to demonstrate the array of structures
#include <stdio.h>
  
// structure template
struct Employee {
    char Name[20];
    int employeeID;
    int WeekAttendence[7];
};
  
// driver code
int main()
{
    // defining array of structure of type Employee
    struct Employee emp[5];
  
    // adding data
    for (int i = 0; i < 5; i++) {
        emp[i].employeeID = i;
        strcpy(emp[i].Name, "Amit");
        int week;
        for (week = 0; week < 7; week++) {
            int attendence;
            emp[i].WeekAttendence[week] = week;
        }
    }
    printf("\n");
  
    // printing data
    for (int i = 0; i < 5; i++) {
        printf("Emplyee ID: %d - Employee Name: %s\n",
               emp[i].employeeID, emp[i].Name);
        printf("Attendence\n");
        int week;
        for (week = 0; week < 7; week++) {
            printf("%d ", emp[i].WeekAttendence[week]);
        }
        printf("\n");
    }
  
    return 0;
}


Output

Emplyee ID: 0 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 1 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 2 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 3 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6
Emplyee ID: 4 - Employee Name: Amit
Attendence
0 1 2 3 4 5 6


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads