![]() |
VOOZH | about |
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.
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.
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];
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.
Following are the basic operations on array of structures:
1 a 2 b 10 A 20 B 10 A 2 b
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,
b Z
The array of structure can be easily traversed in the same way we traverse the array.
1 a 2 b 3 c 4 d 5 e
The below code demonstrates the application of array of structure inside a C program:
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
The array of structures are also affected by the concept called structure padding.
180