How to Insert an Element into an Array of Structs at a Specific Position in C? (original) (raw)
Last Updated : 22 Feb, 2024
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
- 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.
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)
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
- C Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read
- How to Sort an Array of Structs with qsort in C? Sorting C arrays of structs becomes important for many kinds of applications and one common library function that may be used for this is qsort. In this article, we will learn how to use qsort() with an array of structs in C. For Example, Input: struct Person people[] = {{"Person1", 21}, {"Person2", 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