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

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
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
how to remove an element from an array?

Think about the question you are asking. You are not asking to remove elements from the array, you are asking how to not have the computer select pre-selected Pokemon, or to not allow the user to select pre-selected Pokemon.

The first case (Computer selects after user)-you have a randomizer function that can choose any number in the array except those that are already in use. Sounds a bit like an if/else pair.

Same thing for the user-the user is effectively a random number generator that needs feedback if a pre-chosen value is selected. Also if/else, plus maybe a printf or two.

More on reddit.com
๐ŸŒ r/C_Programming
18
10
October 30, 2018
C++: How to delete an element in an array?
An array is really simple. You have to do everything the hard way. Imagine allocating an array of size 5 is like buying a coat rack with five hooks in a row . Now suppose you hang up five coats: A, B, C, D, E. If you remove the coat in the middle, C - you'll have to move D and E over to the left. There's no magic way to do it. Move one, then move the other. You can't make an array smaller, just like you can't make a coatrack smaller. You could saw it, but it'd have an unfinished edge - basically you can't just "resize it". Arrays are like that. So why would anyone use an array? Well, at a really low level that's how computers work. They just have a bunch of memory. An array is just a contiguous range of memory. C++ and most other programming languages have much nicer data types to work with, like std::vector, for example. Unlike an array, a vector can be resized. You can insert or delete from the middle. A vector has a hidden array inside. When you resize the vector, it sometimes has to delete one array and allocate a new one - but all of that is hidden from you. Hope that helps! More on reddit.com
๐ŸŒ r/learnprogramming
9
2
December 7, 2019
๐ŸŒ
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.
๐ŸŒ
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; }
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
ProCoding
procoding.org โ€บ c-program-delete-an-element-from-array
C Program to delete an element from array - procoding.org
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Techcrashcourse
techcrashcourse.com โ€บ 2015 โ€บ 03 โ€บ c-program-to-delete-element-from-an-array.html
C Program to Delete an Element from an Array
Here is the C program to delete an element from an array. Given an array of length N, we have to delete an element at index i (0 <= i <= N-1) from an array. After deleting an element, the number of elements in array will decrease by one. All elements of array are stored in contiguous memory ...
๐ŸŒ
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.
๐ŸŒ
Cprogrammingcode
cprogrammingcode.com โ€บ 2017 โ€บ 06 โ€บ c-program-to-delete-element-from-array.html
Programming Tutorials: C Program to Delete an Element from an Array
Let's write a c code to delete an element from an array. #include <stdio.h> int main() { int arr[100], n, i, pos; printf("Enter the size of an array( Max 100) \n"); scanf("%d", &n); printf("Enter an array elements \n"); //Take input values for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter position to delete an element\n"); scanf("%d", &pos); //Check valid delete positon if(pos < 0 || pos > n ) { printf("Invalid position"); } else { //Traverse an array for(i = pos-1; i < n; i++) { arr[i] = arr[i+1]; } //Decrement the size of an array n--; printf("Array after deleting an element\n")
๐ŸŒ
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; }