1) If the declartion of your array was done in this way:

int A[20];

Your array could not be deleted because it's allocated statically

2) If the declartion of your array was done in this way:

int *A=malloc(20 * sizeof(int));
//or
int *A=calloc(20, sizeof(int));

Your array could be deleted with free(A) because it's allocated dynamically

Answer from MOHAMED on Stack Overflow
🌐
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 - To delete an element 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.
Discussions

Removing elements from an array in C - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
Howw to remove an element from an Array in C ?
As u/MaurauderIIC says, C arrays are of a fixed size, and using malloc() to make a modified copy may be the simplest way. You may also want to look into linked lists, which are a simple implementation of an ordered, dynamically-sized list that can easily be made to have functions capable of insertion and deletion... More on reddit.com
🌐 r/C_Programming
9
2
July 1, 2016
How to delete an element from an array in C? - Stack Overflow
I've tried shifting elements backwards but it is not making the array completely empty. for(i=pos;i More on stackoverflow.com
🌐 stackoverflow.com
How does delete[] know how long the array is?
This is a good question. The memory allocator does in fact keep track of it for you. However ..... keep in mind that the allocator can reserve a space larger than the memory you requested. For instance say you need an integer array of 10 length, and the allocator finds an unused slot that would fit an integer array of 15. It may figure it's not worth breaking that piece of memory up so it just hands you the whole thing. Also, it may not and likely doesn't keep track of how much you originally wanted, only the size of the piece it actually gave you. This can of course vary with different implementations and even different calls to the allocator so it really doesn't know the exact size of your array. It knows some size which is at least the size of your array. More on reddit.com
🌐 r/cpp_questions
49
36
June 22, 2021
🌐
Sanfoundry
sanfoundry.com › c-program-delete-specified-integer-array
C program to Delete an Element from an Array - Sanfoundry
May 15, 2023 - 1. The program will ask the user to enter the size of the array and then the elements of the array and store it in n and &arr[i]. 2. Ask the user to enter the index of the element to be deleted. 3. Loop through the array and delete the element from the array. 4. 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.
🌐
W3Schools
w3schools.in › c-programming › examples › delete-element-from-array
C Program to Delete an Element from an Array - W3schools
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; }
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.

🌐
OpenGenus
iq.opengenus.org › delete-array-in-c
Delete an array in C
March 31, 2020 - In the above example, the whole array a is passed as an argument to the in-built function free which deallocates the memory that was assigned to the array a. However, if we allocate memory for each array element, then we need to free each element before deleting the array. char ** a = malloc(10*sizeof(char*)); for(int i=0; i < 10; i++) { a[i] = malloc(i+1); } for (int i=0; i < 10; i++) { free(a[i]); } free(a);
🌐
Programming Simplified
programmingsimplified.com › c › source-code › c-program-delete-element-from-array
C program to delete an element from an array | Programming Simplified
It also checks whether deletion is possible or not, for example, if an array contains five elements and user wants to delete the element at the sixth position, it isn't possible. ... 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
🌐
Reddit
reddit.com › r/c_programming › howw to remove an element from an array in c ?
r/C_Programming on Reddit: Howw to remove an element from an Array in C ?
July 1, 2016 - As u/MaurauderIIC says, C arrays are of a fixed size, and using malloc() to make a modified copy may be the simplest way. You may also want to look into linked lists, which are a simple implementation of an ordered, dynamically-sized list that can easily be made to have functions capable of insertion and deletion...
🌐
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.
🌐
CODEDOST
codedost.com › home › array&pointers in c › c program to delete an element from an array
C program to delete an element from an array | CODEDOST
July 15, 2018 - #include<stdio.h> void main() { int a[100],i,n,pos; printf("\nEnter no of elements\n"); scanf("%d",&n); printf("Enter the elements\n"); for (i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Elements of array are\n"); for(i=0;i<n;i++) { printf("a[%d] = %d\n",i,a[i]); } printf("Enter the position from which the number has to be deleted\n"); scanf("%d",&pos); for(i=pos;i<n-1;i++) { a[i]=a[i+1]; } n=n-1; printf("\nOn Deletion, new array we get is\n"); for(i=0;i<n;i++) { printf("a[%d] = %d\n",i,a[i]); } } Mastering the Skillset: Essentials for a Future in Data Science and Business Analytics ... AI BAKKT
🌐
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.
🌐
Tutorial Gateway
tutorialgateway.org › c-program-delete-element-array
C Program to Delete an Element in an Array
April 5, 2025 - Please Enter Number of elements in an array : 4 Please Enter 4 elements of an Array 25 46 78 96 Please Enter a Valid Index Position of a Element that you want to Delete : 4 Please Enter a Valid Index Position between 0 and 3 Final Array after Deleteing an Array Elemnt is: 25 46 78 96
🌐
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....
🌐
CodeChef
codechef.com › learn › course › college-data-structures-c › CPDSC01 › problems › DSAC25
Deletion from an array in Data Structures using C
Deleting elements from a 1D array involves removing an element from a specific position within the array and then shifting the remaining elements to fill the gap.
🌐
Quora
quora.com › How-do-I-empty-an-array-in-C
How to empty an array in C - Quora
Answer (1 of 14): You can set the values to zero or NULL (or blank if it is an array of characters, maybe?) using memset or using a for loop. Or just by setting each element to NULL: myArray[0] = 0; myArray[1]=0; etc. Or maybe what you mean by “empty” is to deallocate the memory allocated ...
🌐
GitHub
github.com › kaisarmasum › C-programming-Example › blob › master › delete array.c
C-programming-Example/delete array.c at master · kaisarmasum/C-programming-Example
int a[100],p,i,n; · printf("Enter the element of array:\n"); · scanf("%d",&n); · printf("\nEnter %d element:\n",n); · for(i=0;i<n;i++) · { · scanf("%d",&a[i]); · } · printf("Enter the location to be deleted:\n"); ·
Author   kaisarmasum
🌐
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
🌐
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
🌐
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