There are really two separate issues. The first is keeping the elements of the array in proper order so that there are no "holes" after removing an element. The second is actually resizing the array itself.

Arrays in C are allocated as a fixed number of contiguous elements. There is no way to actually remove the memory used by an individual element in the array, but the elements can be shifted to fill the hole made by removing an element. For example:

void remove_element(array_type *array, int index, int array_length)
{
   int i;
   for(i = index; i < array_length - 1; i++) array[i] = array[i + 1];
}

Statically allocated arrays can not be resized. Dynamically allocated arrays can be resized with realloc(). This will potentially move the entire array to another location in memory, so all pointers to the array or to its elements will have to be updated. For example:

remove_element(array, index, array_length);  /* First shift the elements, then reallocate */
array_type *tmp = realloc(array, (array_length - 1) * sizeof(array_type) );
if (tmp == NULL && array_length > 1) {
   /* No memory available */
   exit(EXIT_FAILURE);
}
array_length = array_length - 1;
array = tmp;

realloc() will return a NULL pointer if the requested size is 0, or if there is an error. Otherwise, it returns a pointer to the reallocated array. The temporary pointer is used to detect errors when calling realloc() because instead of exiting, it is also possible to just leave the original array as it was. When realloc() fails to reallocate an array, it does not alter the original array.

Note that both of these operations will be fairly slow if the array is large or if a lot of elements are removed. There are other data structures like linked lists and hashes that can be used if efficient insertion and deletion is a priority.

Answer from Ben on Stack Overflow
Top answer
1 of 6
27

There are really two separate issues. The first is keeping the elements of the array in proper order so that there are no "holes" after removing an element. The second is actually resizing the array itself.

Arrays in C are allocated as a fixed number of contiguous elements. There is no way to actually remove the memory used by an individual element in the array, but the elements can be shifted to fill the hole made by removing an element. For example:

void remove_element(array_type *array, int index, int array_length)
{
   int i;
   for(i = index; i < array_length - 1; i++) array[i] = array[i + 1];
}

Statically allocated arrays can not be resized. Dynamically allocated arrays can be resized with realloc(). This will potentially move the entire array to another location in memory, so all pointers to the array or to its elements will have to be updated. For example:

remove_element(array, index, array_length);  /* First shift the elements, then reallocate */
array_type *tmp = realloc(array, (array_length - 1) * sizeof(array_type) );
if (tmp == NULL && array_length > 1) {
   /* No memory available */
   exit(EXIT_FAILURE);
}
array_length = array_length - 1;
array = tmp;

realloc() will return a NULL pointer if the requested size is 0, or if there is an error. Otherwise, it returns a pointer to the reallocated array. The temporary pointer is used to detect errors when calling realloc() because instead of exiting, it is also possible to just leave the original array as it was. When realloc() fails to reallocate an array, it does not alter the original array.

Note that both of these operations will be fairly slow if the array is large or if a lot of elements are removed. There are other data structures like linked lists and hashes that can be used if efficient insertion and deletion is a priority.

2 of 6
11

What solution you need depends on whether you want your array to retain its order, or not.

Generally, you never only have the array pointer, you also have a variable holding its current logical size, as well as a variable holding its allocated size. I'm also assuming that the removeIndex is within the bounds of the array. With that given, the removal is simple:

Order irrelevant

array[removeIndex] = array[--logicalSize];

That's it. You simply copy the last array element over the element that is to be removed, decrementing the logicalSize of the array in the process.

If removeIndex == logicalSize-1, i.e. the last element is to be removed, this degrades into a self-assignment of that last element, but that is not a problem.

Retaining order

memmove(array + removeIndex, array + removeIndex + 1, (--logicalSize - removeIndex)*sizeof(*array));

A bit more complex, because now we need to call memmove() to perform the shifting of elements, but still a one-liner. Again, this also updates the logicalSize of the array in the process.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-delete-an-element-in-an-array
C Program to Delete an Element in an Array - GeeksforGeeks
April 2, 2025 - But we can logically delete the ... using its value from an array, first find the position of the element using a loop and then shift all the element to the right of it one position to the left....
๐ŸŒ
log2base2
log2base2.com โ€บ data-structures โ€บ array โ€บ remove-a-specific-element-from-array.html
Remove a specific element from array
/* * Program : Remove an element in the array * Language : C */ #include<stdio.h> #define size 5 int main() { int arr[size] = {1, 20, 5, 78, 30}; int key, i, index = -1; printf("Enter element to delete\n"); scanf("%d",&key); /* * iterate the array elements using loop * if any element matches the key, store the index */ for(i = 0; i < size; i++) { if(arr[i] == key) { index = i; break; } } if(index != -1) { //shift all the element from index+1 by one position to the left for(i = index; i < size - 1; i++) arr[i] = arr[i+1]; printf("New Array : "); for(i = 0; i < size - 1; i++) printf("%d ",arr[i]); } else printf("Element Not Found\n"); return 0; } Run it
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-deleting-an-element-from-array-using-c-123230
Deleting an Element From Array Using C
The program to delete an element ... deleted as inputs, a for loop is used to traverse the array, then the position or value of the targeted element is found and then a loop is used to delete the element....
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-program-delete-specified-integer-array
C program to Delete an Element from an Array - Sanfoundry
May 15, 2023 - If number is not present in the array, then print appropriate message. 7. Run a for loop from that saved location to the size of array, shifting each element of the array leftwards by one. This way the number gets deleted. 9. Print the array. Time Complexity: O(n) The above program for deleting an element in an array has a time complexity of O(n), as the for loop runs from 0 to n.
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to delete element from an array
C program to delete element from an array - Codeforwin
July 20, 2025 - Copy the next element to the current element of array. Which is you need to perform array[i] = array[i + 1]. Repeat above steps till last element of array. Finally decrement the size of array by one.
๐ŸŒ
Programming Simplified
programmingsimplified.com โ€บ c โ€บ source-code โ€บ c-program-delete-element-from-array
C program to delete an element from an array | Programming Simplified
if (position >= n+1) printf("Deletion not possible.\n"); else { for (c = position - 1; c < n - 1; c++) array[c] = array[c+1]; ... Download Delete element from array program. You may have observed that we need to shift array elements which are after the element to be deleted, it's very inefficient if the size of the array is large or we need to remove elements from an array repeatedly.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.in โ€บ c-programming โ€บ examples โ€บ delete-element-from-array
C Program to Delete an Element from an Array
For example if array is containing five elements and you want to delete element at position six which is not possible. ... #include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; }
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ array โ€บ c-array-exercise-15.php
C Program: Delete an element at a specified position
Delete an element at desired position from an array : --------------------------------------------------------- Input the size of array : 5 Input 5 elements in the array in ascending order: element - 0 : 1 element - 1 : 2 element - 2 : 3 element - 3 : 4 element - 4 : 5 Input the position where to delete: 3 The new list is : 1 2 4 5 ... Write a C program to delete an element from an array at a given position using pointer arithmetic.
๐ŸŒ
ProCoding
procoding.org โ€บ c-program-delete-an-element-from-array
C Program to delete an element from array | ProCoding
May 13, 2020 - An element can be deleted by shifting all the next element back to one position. #include <stdio.h> void main() { int arr[50], size, pos, i; printf("Enter number of elements: "); scanf("%d", &size); printf("Enter array elements-\n"); for (i ...
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-program-delete-element-array
C Program to Delete an Element in an Array
April 5, 2025 - How do you write a C Program to Delete an Element in an Array using a for loop and if else statement with an example.? This program to delete elements allows users to enter the One Dimensional Arrayโ€™s size and row elements. Next, we are using For Loop to find and delete an element from it at specified index positions.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ remove-an-element-from-an-array-in-c
Remove an element from an array in C - javatpoint
Remove an element from an array in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c union, c strings etc.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ delete-element-from-array-of-structs-in-c
How to Delete an Element from an Array of Structs in C? - GeeksforGeeks
July 23, 2025 - The below example demonstrates how we can remove the element from an array of structures in C. ... // C program to delete an element from an array of structure #include <stdio.h> #include <string.h> struct Person { char name[50]; int age; }; #define MAX_PERSONS 10 // function to print the content of array of structure void displayPersons(struct Person persons[], int count) { printf("Array of Persons after deletion:\n"); for (int i = 0; i < count; ++i) { printf("Name: %s, Age: %d\n", persons[i].name, persons[i].age); } printf("\n"); } int main() { // intializing array of structure struct Person
๐ŸŒ
CodeChef
codechef.com โ€บ learn โ€บ course โ€บ college-data-structures-c โ€บ CPDSC01 โ€บ problems โ€บ DSAC25
Deletion from an array in Data Structures using C
Test your Data Structures using C knowledge with our Deletion from an array practice problem. Dive into the world of college-data-structures-c challenges at CodeChef.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ deleting-elements-in-an-array-array-operations
Deleting Elements in an Array - Array Operations - GeeksforGeeks
July 23, 2025 - To delete an element from the beginning of an array, we need to shift all subsequent elements one position to the left to fill the gap created by the removal. After shifting all the elements, reduce the array size by 1 to remove the extra element ...
๐ŸŒ
Quora
quora.com โ€บ How-can-I-delete-an-element-in-an-array-using-C-programming
How to delete an element in an array using C programming - Quora
Answer (1 of 2): This depend on what you means by โ€œdelete.โ€ Are you referring to the element itself or are you referring to the element and its position in the array? If the element you need to delete owns memory and does not share the memory I would free the memory before deleting the element to...
๐ŸŒ
Studytonight
studytonight.com โ€บ c โ€บ programs โ€บ array โ€บ deleting-an-element-from-array
Program to Delete an Element from Array in C | Studytonight
#include<stdio.h> int main() { printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); int array[100], position, c, n; 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 delete element from: "); scanf("%d", &position); if(position >= n+1) printf("\n\nDeletion not possible\n\n"); else // updating the locations with next elements for(c = position-1; c < n-1; c++) array[c] = array[c+1]; printf("\n\nResultant array is: "); /* the array size gets reduced by 1 after deletion of the element */ for(c = 0; c < n-1; c++) printf("%d ", array[c]); printf("\n\n\t\t\tCoding is Fun !\n\n\n"); return 0; }