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 of Strings in C In C, an array of strings is a 2D array where each row contains a sequence of characters terminated by a '\0' NULL character (strings). It is used to store multiple strings in a single array.Let's take a look at an example:C#include <stdio.h> int main() { // Creating array of strings for 3 str
3 min read
Properties of Array in C An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe
8 min read
Array of Pointers in C In C, a pointer array is a homogeneous collection of indexed pointer variables that are references to a memory location. It is generally used in C Programming when we want to point at multiple memory locations of a similar data type in our C program. We can access the data by dereferencing the point
6 min read
Structure Pointer in C A structure pointer is a pointer variable that stores the address of a structure. It allows the programmer to manipulate the structure and its members directly by referencing their memory location rather than passing the structure itself. In this article let's take a look at structure pointer in C.L
3 min read
C Structures In C, a structure is a user-defined data type that can be used to group items of possibly different types into a single type. The struct keyword is used to define a structure. The items in the structure are called its member and they can be of any valid data type.Example:C#include <stdio.h> //
9 min read
Pointer vs Array in C Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being:  1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2.
1 min read
Pointers vs Array in C++ Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from e
3 min read
Difference between Structure and Array in C Array in C An array is collection of items stored at contiguous memory locations. Structure in C A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Difference between Structure and Array AR
2 min read
Array of Structures vs Array within a Structure in C Both Array of Structures and Array within a Structure in C programming is a combination of arrays and structures but both are used to serve different purposes.Array within a StructureA structure is a data type in C that allows a group of related variables to be treated as a single unit instead of se
5 min read
How to store words in an array in C? We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index
2 min read