An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C.
Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos.
Here’s how to do it.
- First get the element to be inserted, say x
- Then get the position at which this element is to be inserted, say pos
- Then shift the array elements from this position to one position forward, and do this for all the other elements next to pos.
- Insert the element x now at the position pos, as this is now empty.
Below is the implementation of the above approach:
1 2 3 4 5 6 7 8 9 10 1 2 3 4 50 5 6 7 8 9 10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum element in a very large array using pthreads
- What’s difference between “array” and “&array” for “int array” ?
- C program to detect tokens in a C program
- C program to print a string without any quote (singe or double) in the program
- Hello World Program : First program while learning Programming
- Program to print Sum of even and odd elements in an array
- Program to copy the contents of one array into another in the reverse order
- C program to sort an array using pointers
- C program to sort an array in ascending order
- C program to traverse an Array
- Pointer to an Array | Array Pointer
- Difference between pointer to an array and array of pointers
- Jagged Array or Array of Arrays in C with Examples
- Array of Structures vs. Array within a Structure in C/C++
- Program for Sum of the digits of a given number
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Output of the program | Dereference, Reference, Dereference, Reference....
- Quine - A self-reproducing program
- Program to compute Log n
- Program to print last 10 lines
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.