C Program to Insert an Element in an Array

C Program to Insert an Element in an Array

Inserting an element into an array is a common task in C programming. It allows you to add new data at a specific position without overwriting existing elements. Understanding how to insert elements helps beginners grasp array indexing, memory management, and shifting elements, which are fundamental skills for building more complex programs. In this tutorial, we will learn how to insert an element at any position in an array while preserving the existing elements.

Arrays in C have a fixed size, which means you must ensure there is enough space to add a new element. Inserting an element involves shifting elements to the right, making room for the new value. We will explore step-by-step how to implement this and explain each part of the code to help you fully understand the process.

Inserting an Element Using a Loop

The simplest way to insert an element is to shift all elements after the insertion point one position to the right. This creates space for the new element.

#include <stdio.h>

int main() {

    int n, i, pos, value;

    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    int arr[100]; // Declaring array with extra space

    printf("Enter %d elements: ", n);

    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Enter the position to insert (1 to %d): ", n+1);
    scanf("%d", &pos);

    printf("Enter the value to insert: ");
    scanf("%d", &value);

    // Shift elements to the right
    for(i = n; i >= pos; i--) {
        arr[i] = arr[i - 1];
    }

    // Insert the new element
    arr[pos - 1] = value;

    n++; // Increase array size

    printf("Array after insertion: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;

}

In this program, we first read the array size and elements. The user specifies the position to insert a new value. We then use a for loop starting from the end of the array to the insertion position, moving each element one step to the right. This prevents overwriting elements. Finally, we place the new value at the desired position and print the updated array.

Inserting Using memmove for Efficient Shifting

For larger arrays, shifting elements manually using a loop can be inefficient. The standard library function memmove can move a block of memory, making insertion faster and cleaner.

#include <stdio.h>
#include <string.h>

int main() {

    int n, pos, value;

    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    int arr[100]; // Extra space

    printf("Enter %d elements: ", n);

    for(int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Enter the position to insert (1 to %d): ", n+1);
    scanf("%d", &pos);

    printf("Enter the value to insert: ");
    scanf("%d", &value);

    // Move memory block to make space
    memmove(&arr[pos], &arr[pos - 1], (n - pos + 1) * sizeof(int));

    // Insert new value
    arr[pos - 1] = value;

    n++; // Update array size

    printf("Array after insertion: ");

    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;

}

Here, memmove(&arr[pos], &arr[pos - 1], (n - pos + 1) * sizeof(int)) moves all elements from the insertion position to the end of the array one position to the right. This is safer than memcpy when the source and destination overlap, which happens in insertion. After shifting, we place the new value at the correct position and print the array.

FAQs

Q1: Can I insert multiple elements at once?
Yes, but you need to shift space for all new elements and place them sequentially. Using memmove can make this easier.

Q2: Can this method be used for arrays of floats or characters?
Yes, the logic is the same. Just change the array type and sizeof() accordingly.

Q3: What happens if the array is full?
You must allocate extra space when declaring the array. In C, arrays cannot dynamically resize without using pointers and dynamic memory (malloc).

Q4: Why use memmove instead of memcpy?
memmove handles overlapping memory blocks safely. In insertion, the source and destination overlap, so memmove prevents data corruption.

Conclusion

Inserting an element into an array is a fundamental operation in C. Beginners should first practice using loops to understand array indexing and element shifting. Later, using memmove helps perform insertion efficiently and safely. Mastering these techniques builds a strong foundation for advanced data manipulation, such as in dynamic arrays or sorting algorithms.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. GeeksforGeeks: Insert an Element in an Array – Step-by-step examples.
  3. TutorialsPoint: Arrays in C – Comprehensive array tutorials.
  4. Programiz: C Programming Examples – Beginner-friendly examples.
Scroll to Top