Open In App

Array of Structures in C

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

An array of structures is simply an array where each element is a structure. It allows you to store several structures of the same type in a single array.

Let's take a look at an example:

C
#include <stdio.h>
#include <string.h>

// Structure definition
struct A {
    int var;
};

int main() {
  
    // Declare an array of structures
    struct A arr[2];
  
	arr[0].var = 10;
    arr[1].var = 20;

    for (int i = 0; i < 2; i++)
        printf("%d\n", arr[i].var);

    return 0;
}

Output
10
20

Explanation: We define a Person structure with name and age as members. An array of 2 Person structures is declared, and each element is populated with different people’s details. A for loop is used to iterate through the array and print each person's information.

Array of Structure Declaration

Once you have already defined structure, the array of structure can be defined in a similar way as any other variable.

struct struct_name arr_name [size];

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.

  • Arrays of structures allow you to group related data together and handle multiple instances of that data more efficiently.
  • It is particularly useful when you want to manage large datasets (such as a list of employees, students, or products) where each element has a consistent structure.

Basic Operations on Array of Structures

Following are the basic operations on array of structures:

Initialization

A structure can be initialized using initializer list and so can be the array. Therefore, we can initialize the array of structures using nested initializer list:

struct struct_name arr_name [size] = {
{element1_value1, element1_value2, ....},
{element2_value1, element2_value2, ....},
......
......
};

We can also skip the nested braces, but it is not recommended as we can easily lose the count of the element/member and may mess up the initialization.

struct struct_name arr_name [size]= {
element1_value1, element1_value2 ....,
element2_value1, element2_value2 .....
};

GNU C compilers support designated initialization for structures. So we can also use this in the initialization of an array of structures:

struct struct_name arr_name [size] = {
{.element3 = value, .element1 = value, ....},
{.element2 = value, .elementN = value, ....},
......
......
};

Let's take a look at an example of all these initializations:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

//Driver Code Ends }

// Structure definition
struct A {
    int var;
  	char c;
};

int main() {
  
    // Declaration and initialization using nested
  	// initializer list
    struct A arr1[2] = { {1, 'a'}, {2, 'b'} };
  
    // Declaration and initialization using non-nested
  	// initializer list
  	struct A arr2[2] = { 10, 'A', 20, 'B' };
  
	// Designated initialization
  	struct A arr3[2] = { {.c = 'A', .var = 10},
                        {.var = 2, .c = 'b'} };

//Driver Code Starts{

    for (int i = 0; i < 2; i++)
        printf("%d %c
", arr1[i].var, arr1[i].c);
    printf("
");
  
  	for (int i = 0; i < 2; i++)
        printf("%d %c
", arr2[i].var, arr2[i].c);
    printf("
");
  
  	for (int i = 0; i < 2; i++)
        printf("%d %c
", arr3[i].var, arr3[i].c);
    printf("
");

    return 0;
}

//Driver Code Ends }

Output
1 a
2 b

10 A
20 B

10 A
2 b

Access and Update Members

To access the members of a structure in an array, use the index of the array element followed by the dot (.) operator. The general syntax is:

arr_name[index].member_name

where,

  • arr_name: The name of the array of structures.
  • index: The index of the structure element in the array.
  • member_name: The member within the structure you want to access.
C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

// Structure definition
struct A {
    int var;
  	char c;
};

int main() {
    struct A arr[2] = { {1, 'a'}, {2, 'b'} };
  
  	
	// Access the member c of second element of arr
//Driver Code Ends }

    printf("%c
",arr[1].c);
  
  	// Update the value and access again
  	arr[1].c = 'Z';
  	printf("%c",arr[1].c);

//Driver Code Starts{
 

    return 0;
}

//Driver Code Ends }

Output
b
Z

Traversal

The array of structure can be easily traversed in the same way we traverse the array.

C
#include <stdio.h>
#include <string.h>

// Structure definition
struct A {
    int var;
  	char c;
};

int main() {
    struct A arr[5] = { {1, 'a'}, {2, 'b'}, {3, 'c'},
                       {4, 'd'}, {5, 'e'} };
  
  	// Traverse arr
  	for (int i = 0; i < 5; i++)
      	printf("%d %c\n", arr[i].var, arr[i].c);

    return 0;
}

Output
1 a
2 b
3 c
4 d
5 e

Example of Array of Structure

The below code demonstrates the application of array of structure inside a C program:

Store Student Information

C
#include <stdio.h>
#include <string.h>

// Structure definition
struct Student {
    char name[50];
    int age;
    float marks;
};

int main() {
  
    // Declaration and initialization of an array of structures
    struct Student students[3] = {
        {"Nikhil", 20, 85.5},
        {"Shubham", 22, 90.0},
        {"Vivek", 25, 78.0}
    };

    // Traversing through the array of structures and displaying the data
    for (int i = 0; i < 3; i++) {
        printf("Student %d:\n", i+1);
        printf("Name: %s\n", students[i].name);
        printf("Age: %d\n", students[i].age);
        printf("Marks: %.2f\n\n", students[i].marks);
    }

    return 0;
}

Output
Student 1:
Name: Nikhil
Age: 20
Marks: 85.50

Student 2:
Name: Shubham
Age: 22
Marks: 90.00

Student 3:
Name: Vivek
Age: 25
Marks: 78.00

Find the Size of Array of Structures

The array of structures are also affected by the concept called structure padding.

C++
#include <stdio.h>
#include <string.h>

struct Student {
    char name[50];
    int age;
    float marks;
};

int main() {
    struct Student students[3] = {
        {"John", 20, 85.5},
        {"Alice", 22, 90.0},
        {"Bob", 25, 78.0}
    };

    // Printing size of students
    printf("%ld", sizeof(students));

    return 0;
}

Output
180



Next Article
Article Tags :
Practice Tags :

Similar Reads