How to Search in Array of Struct in C? (original) (raw)
Last Updated : 26 Feb, 2024
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:
**Input:
Person people[3] = { { "Alice", 25 }, { "Bob", 30 }, { "Charlie", 35 } };
searchPerson = "Bob"
**Output:
Person found! Name: Bob, Age: 30
Searching in an Array of Struct in C
To search in an array of structs in C, iterate through each element of the array and compare the search key with the relevant field of each struct. If a match is found, return the index of that element. If no match is found, return a negative value such as -1.
C Program to Search in Array of Struct
C `
// C Program to search in array of struct #include <stdio.h> #include <string.h>
// Define the struct typedef struct { int id; char name[50]; } Student;
int main() { // Array of struct declaration Student students[] = { { 1, "John" }, { 2, "Jane" }, { 3, "Jim" } };
// ID to search
int searchId = 2;
// Searching in the array of struct
int i;
for (i = 0; i < 3; i++) {
if (students[i].id == searchId) {
printf("Student with ID %d is %s\n", searchId,
students[i].name);
break;
}
}
if (i == 3) {
printf("No student found with ID %d\n", searchId);
}
return 0;
}
`
Output
Student with ID 2 is Jane
**Time Complexity: O(n)
**Space Complexity: O(1)
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 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 Pack a Struct in C? In C, when you declare a structure, the compiler allocates memory for its members, and the way it does this can involve adding padding between members for alignment purposes. struct packing refers to the arrangement of the members of a structure in memory so that there is no extra space left. In thi 1 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
- 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 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 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 Sort an Array of Structs Based on a Member in C? In C, we may sometimes need to sort the array of structs based on the values of certain members. In this article, we will learn how to sort a given array of structures based on a specific member in C. For Example, Input: myArrayofStructs[] = {{"Person1", 21, 160.5}, {"Person2", 20, 175.0}, {"Person3 2 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read