Open In App

How to Add an Element to an Array of Structs in C?

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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;
}

Output
Original 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)





Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads