C Program to insert an array element using pointers.

In C programming, inserting elements into an array using pointers involves dynamically allocating memory and manipulating array elements through pointer arithmetic. This technique is useful when you need to add elements at specific positions during runtime.

Syntax

void insertElement(int *array, int size, int position, int element);
int *ptr = (int*)malloc(size * sizeof(int));

Algorithm

The algorithm to insert elements into an array using pointers −

  1. Declare and read the array size
  2. Allocate memory dynamically using malloc()
  3. Input array elements using pointer notation
  4. Read the insertion position and new element
  5. Validate position (must be within array bounds)
  6. Shift elements to the right from insertion point
  7. Insert the new element at the specified position

Example: Complete Array Insertion Program

This program demonstrates proper array insertion with element shifting using pointers −

#include <stdio.h>
#include <stdlib.h>

void insertElement(int *arr, int *size, int position, int element) {
    int i;
    
    /* Shift elements to the right from insertion position */
    for (i = *size; i > position; i--) {
        *(arr + i) = *(arr + i - 1);
    }
    
    /* Insert new element at specified position */
    *(arr + position) = element;
    (*size)++;
}

void displayArray(int *arr, int size) {
    int i;
    printf("Array elements: ");
    for (i = 0; i < size; i++) {
        printf("%d ", *(arr + i));
    }
    printf("<br>");
}

int main() {
    int *arr, size, position, element, i;
    
    printf("Enter array size: ");
    scanf("%d", &size);
    
    /* Allocate memory for size+1 to accommodate new element */
    arr = (int*)malloc((size + 1) * sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed!<br>");
        return 1;
    }
    
    printf("Enter %d elements:<br>", size);
    for (i = 0; i < size; i++) {
        scanf("%d", arr + i);
    }
    
    printf("Original ");
    displayArray(arr, size);
    
    printf("Enter position to insert (0 to %d): ", size);
    scanf("%d", &position);
    
    if (position < 0 || position > size) {
        printf("Invalid position! Must be between 0 and %d<br>", size);
    } else {
        printf("Enter element to insert: ");
        scanf("%d", &element);
        
        insertElement(arr, &size, position, element);
        
        printf("After insertion: ");
        displayArray(arr, size);
    }
    
    free(arr);
    return 0;
}
Enter array size: 5
Enter 5 elements:
10 20 30 40 50
Original Array elements: 10 20 30 40 50 
Enter position to insert (0 to 5): 2
Enter element to insert: 25
After insertion: Array elements: 10 20 25 30 40 50

Key Points

  • Memory Management: Use malloc() to allocate extra space for the new element and always call free().
  • Pointer Arithmetic: Use *(arr + i) notation to access elements through pointers.
  • Element Shifting: Shift elements from right to left to make space for insertion.
  • Bounds Checking: Validate insertion position to prevent array overflow.

Conclusion

Array insertion using pointers in C provides dynamic memory management and efficient element manipulation. Always validate input positions and manage memory properly to avoid runtime errors.

Updated on: 2026-03-15T14:03:55+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements