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 - Explanation: In the given program, the function del() deletes an element from the array arr[] based on the provided key. It first finds the position of the key using a loop. Once found, all elements to the right of the key are shifted one position ...
Discussions

Removing elements from array in C - Stack Overflow
What I am trying to achieve is to loop through an array ONCE and while doing so, also remove elements that my function returns false for. However the code I've written does not work. Any idea would... More on stackoverflow.com
🌐 stackoverflow.com
C delete element of an array - Stack Overflow
How can I delete an element of an array in c? For example when I declare this: int array[3]; What is in these 3 cells when I don't initialize them? For example when I fill these now but want to d... More on stackoverflow.com
🌐 stackoverflow.com
Howw to remove an element from an Array in C ?
Posted by u/__Backslash__ - 2 votes and 9 comments More on reddit.com
🌐 r/C_Programming
9
2
July 2, 2016
[C] How to remove element from 2D array?
english[i][50] = english[i + 1][50]; This doesn't do what you likely think it does - it tries to take the character at index 50 from the i + 1th word and assign it to the character at index 50 of the ith word, but as the indices go from 0 to 49, this actually affects the following word's first character (in the current case; the behaviour is actually undefined). You should instead copy each character from index 0 through 49 (or until the terminating null byte) from one word to the other, requiring another for loop. Alternatively, you could use strcpy or memcpy. More on reddit.com
🌐 r/learnprogramming
5
1
December 19, 2020
🌐
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 - I think you can work out minimum with the hint I gave, but swap should be defined as swap(int*w,int*v) -- can you figure out what its implementation looks like? ... /* a -- pointer to the first element of the array * i -- index of the element to remove * n -- length of the array */ memmove(a+i, a+i+1, (--n - i) * sizeof *a);
🌐
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
🌐
Sanfoundry
sanfoundry.com › c-program-delete-specified-integer-array
C program to Delete an Element from an Array - Sanfoundry
May 15, 2023 - This C program will delete an element from a given array by index position or element value along with a detailed explanation and examples.
🌐
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.
🌐
W3Schools
w3schools.in › c-programming › examples › delete-element-from-array
C Program to Delete an Element from an Array - W3schools
#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; }
Find elsewhere
🌐
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.
🌐
LabEx
labex.io › tutorials › c-deleting-an-element-from-array-using-c-123230
Deleting an Element From Array Using C
The for loop is used to accept input from the user using the scanf function into the array arr. We take the position of the element to be deleted as input from the user. We check if the user input position is valid or not; it should be between 1 and n.
🌐
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 - // 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 persons[MAX_PERSONS] = { { "Person1", 25 }, { "Person2", 30 }, { "Person3", 22 }, }; int MAX_PERSO
🌐
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.
🌐
Tpoint Tech
tpointtech.com › remove-an-element-from-an-array-in-c
Remove an element from an array in C - Tpoint Tech
March 17, 2025 - In this topic, we will learn how to delete or remove a particular element from an array in the C programming language.
🌐
Quescol
quescol.com › home › c program to delete element at end of array
C Program to delete element at end of Array - Quescol
May 8, 2025 - To delete an element from the end of array using c program. we will first reduce the size of an array. After reducing the size last element will automatically deleted.
Top answer
1 of 1
5

(I still need to clean this answer up a bit more later--which I will when I make the time.)

How to "remove an element" from an array in C. You might also call this "filtering an array" in C.

You can't resize statically-allocated arrays in C. Instead, you just keep track of a size variable and adjust it accordingly and only print what you need to.

Approach 1: [excellent] modify original array in-place

[best approach if you are okay modifying the original array]: modify the array in place by picking up only the elements you want to keep, and copying them into the same array from the left to the right

For n elements in the array:
Time complexity: O(n)
Space complexity: O(1n) = O(n)

/// Get the number of elements in an array
#define ARRAY_LEN(array) (sizeof(array)/sizeof(array[0]))

int arr[] = {-3, -2, -1, 0, 1, 2, 3};
print_array(arr, ARRAY_LEN(arr));
size_t arr_len; // this will store the array's new length after modifications to it

// Remove values -1, 0, and +1 from the array
size_t i_write = 0;
for (size_t i_read = 0; i_read < ARRAY_LEN(arr); i_read++)
{
    if (is_it_right(arr[i_read]))
    {
        arr[i_write] = arr[i_read];
        i_write++;
    }
}
arr_len = i_write;

print_array(arr, arr_len);

Sample output:

[-3, -2, -1, 0, 1, 2, 3]
[-3, -2, 2, 3]

Approach 2: [excellent] filter an array into another array

[best approach if you want to leave the original array as-is]

For n elements in the array:
Time complexity: O(n)
Space complexity: O(2n) = O(n)

The easiest approach if you're trying to filter certain values from an array, however, is to just copy to a new array. So, here is a demo of that. In this demo, I begin with an array containing [-3, -2, -1, 0, 1, 2, 3], and I filter out all -1, 0, and +1 values with your little is_it_right() algorithm, to end up with [-3, -2, 2, 3] in the new array when I'm done.

To do that in-place in the same array would require a more-advanced algorithm that would almost certainly have the tradeoff of running slower, but, while not doubling the array space. The below approach will run very fast but I double the array space since I'm copying from one array into another rather than filtering the array in-place.

Anyway, study this carefully:

Run it online on OnlineGDB here.

Or download my full, runnable examples for both approaches below in my eRCaGuy_hello_world repo here: array_filter_and_remove_element.c

#include <stdbool.h>
#include <stdio.h>

/// Get the number of elements in an array
#define ARRAY_LEN(array) (sizeof(array)/sizeof(array[0]))

/// Return false for -1, 0, or +1, and return true otherwise.
bool is_it_right(int i)
{
    if (i/2 == 0)
    {
        return false;
    }
    
    return true;
}

void print_array(int arr[], size_t len)
{
    printf("[");
    for (size_t i = 0; i < len; i++)
    {
        printf("%i", arr[i]);
        if (i < len - 1)
        {
            printf(", ");
        }
    }
    printf("]\n");
}

int main()
{
    int arr[] = {-3, -2, -1, 0, 1, 2, 3};
    int arr_filtered[ARRAY_LEN(arr)];
    size_t arr_filtered_len; 
    
    // Remove values -1, 0, and +1 from the array
    int j = 0;
    for (size_t i = 0; i < ARRAY_LEN(arr); i++) 
    {
        if (is_it_right(arr[i]))
        {
            arr_filtered[j] = arr[i];
            j++;
        }
    }
    arr_filtered_len = j;
    
    print_array(arr, ARRAY_LEN(arr));
    print_array(arr_filtered, arr_filtered_len);
    
    return 0;
}

Output:

[-3, -2, -1, 0, 1, 2, 3]
[-3, -2, 2, 3]

Approach 3: [not good] "erase" an element by left-shifting

[way more complicated and processor-intensive and copy-intensive] filter an array in-place by left-shifting all the elements which lie to the right of an element you remove, each time you remove one element

For n elements in the array:
Time complexity: best-case (nothing to filter out): O(n); worst-case (all elements must be filtered out 1-at-a-time): O(n^2)
Space complexity: O(1n) = O(n)

This approach is way more complicated, more processor-intensive, and more copy-intensive. I recommend the approach above instead.

printf("\n====================================\n"
       "APPROACH 2: FILTER AN ARRAY IN-PLACE\n"
       "====================================\n");

// Remove values -1, 0, and +1 from the array **in place**
size_t arr_len = ARRAY_LEN(arr);
printf("Starting array:\n");
print_array(arr, arr_len);
size_t i = 0;
while (i < arr_len)
{
    if (is_it_right(arr[i]) == false)
    {
        // We don't want to keep this value at `arr[i]`, so overwrite it
        // by shifting all values from `i + 1` to the end of the array to
        // the left by 1 place.
        printf("Removing value `%i` from the array\n", arr[i]);
        size_t i_write = i;
        for (size_t i_read = i_write + 1; i_read < arr_len; i_read++)
        {
            arr[i_write] = arr[i_read];
            i_write++;
            print_array(arr, arr_len);
        }
        arr_len--;
        printf("One element has just been removed. Here is the new array:\n");
        print_array(arr, arr_len);
    }
    else // is_it_right(arr[i]) == true
    {
        // only increment `i` if we did NOT just left-shift a bunch of
        // elements by one index place
        i++;
    }
}
printf("Here is the final, filtered-in-place array:\n");
print_array(arr, arr_len);

Sample output:

====================================
APPROACH 2: FILTER AN ARRAY IN-PLACE
====================================
Starting array:
[-3, -2, -1, 0, 1, 2, 3]
Removing value `-1` from the array
[-3, -2, 0, 0, 1, 2, 3]
[-3, -2, 0, 1, 1, 2, 3]
[-3, -2, 0, 1, 2, 2, 3]
[-3, -2, 0, 1, 2, 3, 3]
One element has just been removed. Here is the new array:
[-3, -2, 0, 1, 2, 3]
Removing value `0` from the array
[-3, -2, 1, 1, 2, 3]
[-3, -2, 1, 2, 2, 3]
[-3, -2, 1, 2, 3, 3]
One element has just been removed. Here is the new array:
[-3, -2, 1, 2, 3]
Removing value `1` from the array
[-3, -2, 2, 2, 3]
[-3, -2, 2, 3, 3]
One element has just been removed. Here is the new array:
[-3, -2, 2, 3]
Here is the final, filtered-in-place array:
[-3, -2, 2, 3]

Again, the full, runnable examples for both approaches above are in my eRCaGuy_hello_world repo here: array_filter_and_remove_element.c

See also

  1. [a similar answer of mine in C++, but for strings--ex: std::string type] Removing all the vowels in a string in c++
🌐
Quora
quora.com › How-do-I-remove-a-blank-element-from-an-array-in-C
How to remove a blank element from an array in C - Quora
Answer (1 of 2): What you mean by “blank element”? There are no blank elements! In simplest case with static arrays like: [code]int Array[] = { 1, 2, 3 }; [/code]there are no blank elements. Array is created in memory (in this case on Stack but does not matter) and has 3 INT elements. It is ...
🌐
GitHub
github.com › YUVARAJ-R-ai › Array-Element-Removal-Cpp-C
GitHub - YUVARAJ-R-ai/Array-Element-Removal-Cpp-C: A comprehensive guide demonstrating how to remove elements from arrays in C and C++, covering static arrays, dynamically allocated arrays, and std::vector with example code and comparisons. · GitHub
#include <iostream> using namespace std; int main() { int* arr = new int[5]{10, 20, 30, 40, 50}; int size = 5; int pos = 2; for (int i = pos; i < size - 1; i++) arr[i] = arr[i + 1]; size--; int* newArr = new int[size]; for (int i = 0; i < size; i++) newArr[i] = arr[i]; delete[] arr; arr = newArr; for (int i = 0; i < size; i++) cout << arr[i] << " "; delete[] arr; return 0; } Resizable array with automatic memory management. Easy insertion and removal. #include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = {10, 20, 30, 40, 50}; vec.erase(vec.begin() + 2); // removes element at index 2 for (int val : vec) cout << val << " "; return 0; }
Author   YUVARAJ-R-ai
🌐
C For Dummies
c-for-dummies.com › blog
Modifying an Array | C For Dummies Blog
The main() function outputs the array’s values before and after it’s modified. The remove_element() function is called to pluck out the fourth element: ... Within the remove_element() function, a for loop processes elements from the passed argument (the element to remove) to the end of ...
🌐
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.
🌐
Quora
quora.com › How-do-you-remove-an-element-from-an-array-in-C
How to remove an element from an array in C++ - Quora
Answer: Logic to remove element from array Move to the specified location which you want to remove in given array. 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...