C Program to Delete an Element from an Array

C Program to Delete an Element from an Array

Deleting an element from an array is a fundamental operation in C programming. It allows you to remove unwanted data and maintain the integrity of your dataset. Understanding how to delete elements is crucial for beginners because it teaches array indexing, shifting elements, and managing memory efficiently. In this tutorial, we will learn how to delete an element from any position in an array, step by step, with clear explanations for each part of the code.

Arrays in C have a fixed size, which means you cannot truly “shrink” the array. Instead, you remove an element by shifting all subsequent elements to the left, effectively overwriting the element to be deleted. This technique ensures that no gaps are left in the array, and the order of elements is preserved.

Deleting an Element Using a Loop

The most straightforward way to delete an element is by manually shifting all elements after the deletion point one position to the left. This overwrites the element you want to remove and closes the gap.

#include <stdio.h>

int main() {

    int n, i, pos;

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

    int arr[100]; // Array with extra space

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

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

    printf("Enter the position of the element to delete (1 to %d): ", n);
    scanf("%d", &pos);

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

    n--; // Decrease array size

    printf("Array after deletion: ");

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

    printf("\n");

    return 0;

}

In this program, we first read the number of elements and the array values. The user specifies the position of the element to delete. Using a for loop, we start from the deletion point and shift each element one position to the left. This overwrites the deleted element. Finally, we reduce the array size by one and print the updated array.

Deleting Using memmove for Efficiency

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

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

int main() {

    int n, pos;

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

    int arr[100]; // Array with extra space
    printf("Enter %d elements: ", n);

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

    printf("Enter the position of the element to delete (1 to %d): ", n);
    scanf("%d", &pos);

    // Use memmove to shift elements left
    memmove(&arr[pos - 1], &arr[pos], (n - pos) * sizeof(int));

    n--; // Reduce array size

    printf("Array after deletion: ");

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

    printf("\n");

    return 0;

}

Here, memmove(&arr[pos - 1], &arr[pos], (n - pos) * sizeof(int)) moves all elements from the position after the deleted element to the left by one position. memmove is preferred over memcpy because it safely handles overlapping memory blocks, which occurs during deletion.

FAQs

Q1: Can I delete multiple elements at once?
Yes, you can. You would need to shift the remaining elements for all the deleted items, or use memmove with a larger block size.

Q2: Can this method be applied to arrays of floats or characters?
Absolutely. The logic remains the same. Just update the array type and sizeof() in memmove.

Q3: What happens if I try to delete an element that does not exist?
You must check the position against the array size. Deleting outside the valid range can crash the program or corrupt memory.

Q4: Why use memmove for deletion?
memmove handles overlapping memory safely. When shifting elements for deletion, the source and destination areas overlap, so memmove prevents data corruption.

Conclusion

Deleting an element from an array is an essential operation for beginners learning C. By first practicing with loops, you gain a clear understanding of array indexing and element shifting. Using memmove allows you to perform deletion more efficiently and safely in larger arrays. Mastering deletion, along with insertion, lays the foundation for dynamic data management and prepares you for more complex programming tasks.

References & Additional Resources

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