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.

🌐
Codeforwin
codeforwin.org › home › c program to delete element from an array
C program to delete element from an array - Codeforwin
July 20, 2025 - Write a C program to delete element from array at specified position. How to remove element from array at given position in C programming. Logic to remove element from any given position in array. The program should also print an error message if the delete position is invalid.
🌐
Cplusplus
cplusplus.com › forum › beginner › 73802
Function push() and pop() - C++ Forum
Make sure the current index is ... (8,8) with index 2 // index down (6,6) with index 3 // index up Pop() - function that decrements the current position and then returns the element at that position....
🌐
PHP
php.net › manual › en › function.array-pop.php
PHP: array_pop - Manual
What would be wrong with: <?php $array = array('one', 'two', 'three', 'four'); //pop the last element off array_pop($array); //$array == array('one', 'two', 'three'); ?> As the documentation clearly notes, array_pop() not only returns the last element, but actually removes it from the array wich is passed by reference.
🌐
Quora
quora.com › How-do-I-write-push-and-pop-functions-in-C
How to write push and pop functions in C - Quora
Answer (1 of 6): In Stack, PUSH & POP are the two main operations. push operation inserts an element in the stack and pop operation deletes an element from the stack. a stack is a linear data structure in which we push the data from one side ...
🌐
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
Top answer
1 of 1
2

Popping an element should remove it from the stack but at the same time return that very same element (whether you delete it or process it further is up to you).

In case you use a static structure such as array for the stack you can simply nullify the content of the cell that represented your stack's top which you have just popped, return that element (if you store by value, then you just return it otherwise return reference) and set the index to the element underneath it as the new top OR create a copy of that array with a smaller size and copy the content that has not been popped there. You can optimize the last method by trying to do popping in a batch that is multiple poppings one after another and then "flush" the stack to a newer smaller one. I personally prefer the first way since a stack is usually considered something with a certain size (=a stack with bounded instead of dynamic capacity).

Removing the element when popping is the way pop in a stack works and if you simply leave the old value there it might for example lead to dumb situation like overwriting an element, that you have popped by a push command but didn't want to overwrite at all. If you do it the proper way you always know that once popped that value can no longer be retrieved (except at the moment of popping it) from that stack because it is no longer there!

Also you can use a single linked list for the implementation and then you can indeed reduce the size of the stack when doing a pop and not just "replace" the popped element with a dummy value or create a new smaller stack and copy the rest there.

Find elsewhere
🌐
Codeforwin
codeforwin.org › home › stack implementation using array, push, pop and display in c
Stack implementation using array, push, pop and display in C - Codeforwin
July 20, 2025 - Increment top by one and push new element to stack using stack[++top] = data; (where data is the new element). Removing an element from stack is referred as Pop operation in stack.
🌐
Programming9
programming9.com › programs › c-programs › 302-implementation-of-stack-using-array-in-c
Implementation of Stack Using Array in C
The C Program is written for implementation of STACK using Array, the basic operations of stack are PUSH(), POP(), both operations will be done from top of the
🌐
Programiz
programiz.com › javascript › library › array › pop
JavaScript Array pop()
let cities = ["Madrid", "New York", "Kathmandu", "Paris"]; // remove the last element let removedCity = cities.pop(); console.log(cities) // ["Madrid", "New York", "Kathmandu"] console.log(removedCity); // Paris ... Here, arr is an array.
🌐
Unreal Engine
forums.unrealengine.com › development › programming & scripting
TArray: question about push, pop, and shrink - Programming & Scripting - Epic Developer Community Forums
November 24, 2014 - Let’s say I have an array that looks like this: ["a", "b", "c", "d"] Now let’s say I pop an element off the array without shrinking. Would the array now look like this? ["a", "b", "c", null] And if I pushed an eleme…
🌐
DigitalOcean
digitalocean.com › community › tutorials › stack-in-c
How to Implement a Stack in C With Code Examples | DigitalOcean
July 23, 2025 - C does not have a built-in stack type; you must implement it yourself, typically using either a fixed-size array or a dynamic linked list. The core operations that define any stack are push (add to top), pop (remove from top), and peek (view top).
🌐
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];
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › pop
Array.prototype.pop() - JavaScript | MDN
The pop() method of Array instances removes the last element from an array and returns that element. This method changes the length of the array.
🌐
w3resource
w3resource.com › cpp-exercises › stack › cpp-stack-exercise-20.php
C++ Implement a stack using a dynamic array with push, pop
C++ Exercises, Practice and Solution: Write a C++ program to implement a stack using a dynamic array with push and pop operations. Find the top element of the stack and check if the stack is empty or not.
🌐
GitHub
gist.github.com › mostafa6765 › 81d5bbb51ef4dfb8738ee13b80920e7d
push ,pop ,display with array c++ cpp · GitHub
push ,pop ,display with array c++ cpp. GitHub Gist: instantly share code, notes, and snippets.