![]() |
VOOZH | about |
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.
We can initialize the array of structures using list initialization where each of the nested lists corresponds to the members of a single structure element.
struct Str str[] = {
{structure1_value1, structure1_value2},
{structure2_value1, structure2_value2},
{structure3_value1, structure3_value2}
};The below example demonstrates how we can initialize an array of structures in C.
Students: Name: John, Roll: 1, Marks: 85.50 Name: Emma, Roll: 2, Marks: 90.60 Name: Harry, Roll: 3, Marks: 92.70
Time Complexity: O(N), where N is the size of the array.
Space Complexity: O(1) as no extra space for utilization is required.