How to Iterate Through Array of Structs in C? (original) (raw)
Last Updated : 01 Mar, 2024
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 Structures
To iterate through an array of the structures in C, we can simply use a loop (for loop or a while loop) and access each struct in the array using the array indexing, and then we can access every member using the member name.
C Program to Iterate Over an Array of Structs
The below example demonstrates how we can iterate through an array of structs and access each struct in C.
C `
// C program to illustrate how to iterate through array of // structs #include <stdio.h>
// Define the Person struct struct Person { int id; char name[20]; int age; };
int main() { // Creating and initializing an array of Person structs struct Person people[] = { { 1, "Ram", 20 }, { 2, "Mohan", 25 }, { 3, "Ria", 30 } };
// Calculating the number of elements in the array of
// struct
int numPeople = sizeof(people) / sizeof(people[0]);
// Iterating through the array of structs
for (int i = 0; i < numPeople; i++) {
printf("Person Id: %d, Name: %s, Age: %d\n",
people[i].id, people[i].name, people[i].age);
}
return 0;
}
`
Output
Person Id: 1, Name: Ram, Age: 20 Person Id: 2, Name: Mohan, Age: 25 Person Id: 3, Name: Ria, Age: 30
**Time Complexity: O(N), here n is the number of elements in the array of structs.
**Auxilliary Space: O(1)
**Note: We can also use a pointer to iterate through the array and access each element's members using the arrow operator (
->
).
Similar Reads
- 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
- 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 Sort an Array of Structs with qsort in C? Sorting C arrays of structs becomes important for many kinds of applications and one common library function that may be used for this is qsort. In this article, we will learn how to use qsort() with an array of structs in C. For Example, Input: struct Person people[] = {{"Person1", 21}, {"Person2", 2 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 Initialize Array of Pointers in C? Arrays are collections of similar data elements that are stored in contiguous memory locations. On the other hand, pointers are variables that store the memory address of another variable. In this article, we will learn how to initialize an array of pointers in C. Initialize Array of Pointers in CWe 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
- How to Declare a Pointer to a Struct in C? Structure (or structs) in the C programming language provides a way to combine variables of several data types under one name and pointers provide a means of storing memory addresses. In this article, we will learn how to declare such a pointer to a struct in C. Declaration of Pointer to Struct in C 2 min read
- How to Add an Element to an Array of Structs in C? In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra 3 min read
- How to Initialize Char Array in Struct in C? In C++, we can also define a character array as a member of a structure for storing strings. In this article, we will discuss how to initialize a char array in a struct in C. Initialization of Char Array in Struct in CWhen we create a structure instance (variable) in C that includes a char array, we 2 min read
- How to Use bsearch with an Array of Struct in C? The bsearch function in C is a standard library function defined in the stdlib.h header file that is used to perform binary search on array-like structure. In this article, we will learn to use bsearch with an array of struct in C. Input: struct Person people[] = { { 1, "Ram" }, { 2, "Rohan" }, { 4, 3 min read