How to Add an Element to an Array of Structs in C?
Last Updated :
18 Mar, 2024
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:
structArray[] = { { "Ram", 21 }, { "Rohit", 25 } };
Element to add: new_person = { "Sia", 24 };
Output:
structArray: Name: Ram, Age: 21
Name: Rohit, Age: 25
Name: Sia, Age: 24
Add an Element to an Array of Structs in C
One structure can be assigned to the other using the assignment operator. We can add elements to the array of structures by assigning them to the required index.
Approach
- Create a new struct element with the data that you want to insert in the array.
- Create a function to add an element.
- Check if the array of struct is full or not.
- If not full, add the new element into the array of struct at the end of the array.
C Program to Add an Element to an Array of Structs in C
The following program illustrates how we can add an element to an array of structs in C.
C
// C Program to illustrate how to add an element to an array
// of struct
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PEOPLE 10
struct Person {
char name[50];
int age;
};
// Function to add an element to an array of structs
void addElement(struct Person new_person,
struct Person people[], int* counter,
int size)
{
// Ensure array bounds are not exceeded
if (*counter < size) {
people[*counter] = new_person;
// Increment the count of element in the array
(*counter)++;
}
else {
printf("Array is full, cannot add more people.\n");
}
}
int main()
{
// Create an array of structs
struct Person people[MAX_PEOPLE]
= { { "Ram", 21 }, { "Rohit", 25 } };
// Counter to denote Number of elements currently in the
// array
int counter = 2;
// Print the original array
printf("Original Array of Struct:\n");
for (int i = 0; i < counter; i++) {
printf("Name: %s, Age: %d\n", people[i].name,
people[i].age);
}
// Create a new struct element to add in the array
struct Person new_person = { "Sia", 24 };
// adding the new elements to the array of structs
addElement(new_person, people, &counter, MAX_PEOPLE);
// Print the modified array
printf("\nAfter Addition Array of struct:\n");
for (int i = 0; i < counter; i++) {
printf("Name: %s, Age: %d\n", people[i].name,
people[i].age);
}
return 0;
}
OutputOriginal Array of Struct:
Name: Ram, Age: 21
Name: Rohit, Age: 25
After Addition Array of struct:
Name: Ram, Age: 21
Name: Rohit, Age: 25
Name: Sia, Age: 24
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
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 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 Write a Struct to a Binary File in C? C file handling allows users to store the data from a C program to a file in either the text or the binary format. In this article, we will learn how to write a struct to a binary file in C. Writing Structure into a Binary File in CTo write a struct to a binary file, we can use the fwrite() function
2 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 Insert an Element into an Array of Structs at a Specific Position in C? In C, structs allow the users to create user-defined data types which can be used to store data of different types in a single unit. In many use cases, we might use an array of structs to store the structs in contiguous memory locations to access them sequentially. In this article, we will learn how
3 min read