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;
}
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 }
Output1 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 }
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;
}
Output1 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:
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;
}
OutputStudent 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;
}
Similar Reads
Array within Structure in C
In C, a structure is a user-defined data type that allows us to combine data of different data types. While an array is a collection of elements of the same type. In this article, we will discuss the concept of an array that is declared as a member of the structure. Array within a Structure An array
3 min read
How to Access Array of Structure in C?
In C, we can create an array whose elements are of struct type. In this article, we will learn how to access an array of structures in C. For Example, Input:myArrayOfStructs = {{'a', 10}, {'b', 20}, {'A', 9}}Output:Integer Member at index 1: 20Accessing Array of Structure Members in CWe can access t
2 min read
How to Create an Array of Structs in C?
In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of
2 min read
Return an Array in C
In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C
6 min read
How to Initialize Array of Structs in C?
In C, arrays are data structures that store the data in contiguous memory locations. While structs are used to create user-defined data types. In this article, we will learn how to initialize an array of structs in C. Initializing Array of Structures in CWe can initialize the array of structures usi
2 min read
Array of Pointers to Strings in C
In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings
2 min read
How to Search in Array of Struct in C?
In C, a struct (short for structure) is a user-defined data type that allows us to combine data items of different kinds. An array of structs is an array in which each element is of struct type. In this article, we will learn how to search for a specific element in an array of structs. Example: Inpu
2 min read
How to Pass Array of Structure to a Function in C?
An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin
2 min read
How to Iterate Through Array of Structs in C?
In C, while working with an array of structs we have to iterate through each struct in an array to perform certain operations or access its members. In this article, we will learn how to iterate through an array of the structs in C. Iterate Over of an Array of StructuresTo iterate through an array o
2 min read
How to Create a Dynamic Array of Structs?
In C, we have dynamic arrays in which we can allocate an array of elements whose size is determined during runtime. In this article, we will learn how to create a dynamic array of structures in C. Create a Dynamic Array of Structs in CA dynamic array of structs in C combines dynamic arrays and struc
2 min read