How to Create an Array of Structs in C? (original) (raw)
Last Updated : 11 Mar, 2024
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 C
To create an array of structs, we first need to define the struct type and then declare an array of that type using the below syntax.
Syntax to Create an Array of Structure in C
// Define the struct
struct StructName {
dataType1 member1;
dataType2 member2;
// more members...
};
// Declare an array of structs
struct StructName arrayName[arraySize];
Here,
StructName
is the name of the struct.dataType1
,dataType2
are the data types of the members of the struct.member1
,member2
are the names of the members of the struct.arrayName
is the name of the array of structs.arraySize
is the size of the array.
C Program to Create an Array of Structs
The below program demonstrates how we can create an array of structs in C.
C `
// C program to demonstrate how to create an array of // structs #include <stdio.h>
// Define the struct struct Student { int id; char name[50]; };
int main() { // Declare the size of the array int size = 5;
// Declare an array of structs
struct Student myArray[size];
// Initialize data to structs present in the array
for (int i = 0; i < size; i++) {
myArray[i].id = i + 1;
snprintf(myArray[i].name, sizeof(myArray[i].name),
"Student%d", i + 1);
}
// Print the data of structs present in the array
printf("Array Elements:\n");
for (int i = 0; i < size; i++) {
printf("Element %d: ID = %d, Name = %s\n", i + 1,
myArray[i].id, myArray[i].name);
}
return 0;
}
`
Output
Array Elements: Element 1: ID = 1, Name = Student1 Element 2: ID = 2, Name = Student2 Element 3: ID = 3, Name = Student3 Element 4: ID = 4, Name = Student4 Element 5: ID = 5, Name = Student5
**Time Complexity: O(N), here N is the number of elements present in the array
**Auxiliary Space: O(N)
Similar Reads
- How to Dynamically Create Array of Structs in C? In C, an array is a data structure that stores the collection of elements of similar types. Structs in C allow the users to create user-defined data types that can contain different types of data items. In this article, we will learn how we can create an array of structs dynamically in C. Dynamicall 2 min read
- How to Create a Dynamic Array of Structs? In C, we have dynamic arrays in which we can allocate an array of elements whose size is determined during runtime. In this article, we will learn how to create a dynamic array of structures in C. Create a Dynamic Array of Structs in CA dynamic array of structs in C combines dynamic arrays and struc 2 min read
- How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 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 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 Delete an Element from an Array of Structs in C? In C, an array of structs refers to the array that stores the structure variables as its elements. In this article, we will learn how to delete an element from an array of structures in C. For Example, Input: struct Person persons[3] = { { "Person1", 25 }, { "Person2", 30 }, { "Person3", 22 }, }; Ta 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
- How to Pass Array of Structure to a Function in C? An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin 2 min read
- 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 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