How to Insert an Element into an Array of Structs at a Specific Position in C? Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like 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 StructsIn 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. ApproachCreate the structure that you want to insert.Shift the elements of the array to make space for the new element.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; } OutputOriginal 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) Comment More infoAdvertise with us Next Article How to Insert an Element into an Array of Structs at a Specific Position in C? R rveerendra400 Follow Improve Article Tags : C Programs C Language C-Arrays C-Structure & Union C Examples +1 More Similar Reads 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 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 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 Search in Array of Struct in C? 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: Inpu 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 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 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 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 C Program To Merge A Linked List Into Another Linked List At Alternate Positions Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5->7->17->13->11 and second is 12->10->2->4->6, the first list should become 5->12->7->10->17->2->13->4- 3 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 Like