کد زیر اضافه کردن یک المان در یک آرایه در یک موقعیت دلخواه را نشان می دهد:
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int array[100], position, c, n, value;
printf("\n\nEnter number of elements in array:");
scanf("%d", &n);
printf("\n\nEnter %d elements\n", n);
for(c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("\n\nEnter the location where you want to insert new element: ");
scanf("%d", &position);
printf("\n\nEnter the value to insert: ");
scanf("%d", &value);
// shifting the elements from (position to n) to right
for(c = n-1; c >= position-1; c--)
array[c+1] = array[c];
array[position - 1] = value; // inserting the given value
printf("\n\nResultant array is: ");
/*
the array size gets increased by 1
after insertion of the element
*/
for(c = 0; c <= n; c++)
printf("%d ", array[c]);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
در واقع کد بالا به این صورت است که ما از کاربر یک آرایه به عنوان ورودی می گیریم و بعد از آن، المان جدیدی که می خواهد اضافه کند و سپس موقعیت المانی که می خواهد اضافه کند را از کاربر می گیریم که در نهایت خروجی کد بالا به صورت زیر خواهد بود:

همانطور که در خروجی بالا می بینید المان های بعد از المان جدید یک شیفت به سمت راست خواهند داشت.