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
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
You can just use free to delete a dynamically allocated array.
int* array = malloc(10*sizeof(int));
...
free(array);
If you allocated memory for each array element, you'll also need to free each element first.
char** array = malloc(10*sizeof(char*));
for (int i=0; i<10; i++) {
array[i] = malloc(i);
}
....
for (int i=0; i<10; i++) {
free(array[i]);
}
free(array);
Removing elements from an array in C - Stack Overflow
Howw to remove an element from an Array in C ?
How to delete an element from an array in C? - Stack Overflow
How does delete[] know how long the array is?
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.
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.