C Program to Insert an Element in an Array
Last Updated :
16 Jan, 2025
In this article, we will learn how to insert an element into an array in C.
C arrays have a fixed size, so we cannot dynamically increase their memory. However, we can insert an element if the array already have enough memory space to accommodate the new elements
Insert Element at Specific Position
To insert an element at a specific position, shift all elements to the right of the specified position one step to the right. This will create an empty space at the specified position. Then, insert the new element at the desired position.
C
#include <stdio.h>
void insert(int arr[], int *n, int pos, int val) {
// Shift elements to the right
for (int i = *n; i > pos; i--)
arr[i] = arr[i - 1];
// Insert val at the specified position
arr[pos] = val;
// Increase the current size
(*n)++;
}
int main() {
int arr[7] = {10, 20, 30, 40, 50};
int n = 5;
int pos = 3;
int val = 25;
// Insert the value at the specified position
insert(arr, &n, pos, val);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Explanation: In the given program, the function insert() shifts all elements starting from the insertion index 3 one step to the right. The new value 25 is then inserted at the desired position, and the size of the array is incremented.
Time Complexity: O(n) (for Shifting) + O(1) (for incrementing size) = O(n)
Auxiliary Space: O(1)
If you want to explore array manipulation and other data structures, the C Programming Course Online with Data Structures provides hands-on examples and exercises.
Insert an Element at the End of an Array
If you are inserting an element at the end, there's no need for shifting elements. You can directly add the element at the next available position and increase the size.
C
#include <stdio.h>
void insertLast(int arr[], int *n, int val) {
// Insert val at last
arr[*n] = val;
// Increase the current size
(*n)++;
}
int main() {
int arr[7] = {10, 20, 30, 40, 50};
int n = 5;
int val = 25;
// Insert the value at the end
insertLast(arr, &n, val);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Time Complexity: O(1) (for decrementing size)
Auxiliary Space: O(1)
Note: Apart from insertion at the end, all cases will be handled same as insertion at specific position. Also, make sure array have enough space to avoid segmentation faults.
Similar Reads
How to store words in an array in C? We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index
2 min read
Implement a Stack in C Programming Stack is the linear data structure that follows the Last in, First Out(LIFO) principle of data insertion and deletion. It means that the element that is inserted last will be the first one to be removed and the element that is inserted first will be removed at last. Think of it as the stack of plate
7 min read
How to declare a Two Dimensional Array of pointers in C? A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo
3 min read
Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe
3 min read
Pointer to an Array | Array Pointer A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
5 min read