Open In App

How to Insert an Element into an Array of Structs at a Specific Position in C?

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 we can insert an element into an array of structs at a specific position in C.

Insert an Element at a Specific Position into Array of Structs

In C, we have to manually insert the element in the array of structs at a specific position. We also need to create a space for the new elements by shifting all the elements to the left.

Approach

  1. Create the structure that you want to insert.
  2. Shift the elements of the array to make space for the new element.
  3. Insert the new element at the desired position in the array.

C Program to Insert an Element into an Array of Structs at a Specific Position

C




// C program to to Insert an Element into an Array of
// Structs at a Specific Position
#include <stdio.h>
  
// Define a struct
struct MyStruct {
    int id;
    char name[50];
};
  
// Function to insert an element at a specific position in
// the array
void insertElement(struct MyStruct array[], int* size,
                   int position, struct MyStruct newElement)
{
    // Check if the position is valid
    if (position < 0 || position > *size) {
        printf("Invalid position for insertion.\n");
        return;
    }
  
    // Shift elements to make space for the new element
    for (int i = *size; i > position; i--) {
        array[i] = array[i - 1];
    }
  
    // Insert the new element
    array[position] = newElement;
  
    // Increment the size of the array
    (*size)++;
}
  
int main()
{
    // Example array of structs
    struct MyStruct myArray[100]
        = { { 1, "Geeks" }, { 2, "for" }, { 3, "Geeks" } };
  
    // Current size of the array
    int size = 3;
  
    // Print the original array
    printf("Original array:\n");
    for (int i = 0; i < size; i++) {
        printf("ID: %d, Name: %s\n", myArray[i].id,
               myArray[i].name);
    }
  
    // Create a new struct element to insert in the array
    struct MyStruct newElement = { 4, "C++" };
  
    // Insert the new element at position 1
    insertElement(myArray, &size, 1, newElement);
  
    // Print the modified array
    printf("\nArray after insertion:\n");
    for (int i = 0; i < size; i++) {
        printf("ID: %d, Name: %s\n", myArray[i].id,
               myArray[i].name);
    }
  
    return 0;
}


Output

Original array:
ID: 1, Name: Geeks
ID: 2, Name: for
ID: 3, Name: Geeks

Array after insertion:
ID: 1, Name: Geeks
ID: 4, Name: C++
ID: 2, Name: for
ID: 3, Name: Geeks

Time Complexity: O(N), where N is the total number of elements in the Array.
Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads