![]() |
VOOZH | about |
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.
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.
- Create 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.
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)