Modifying an Array

I’ve seen functions (okay, methods) in other programming languages that let you digitally tussle with an array. You can split an array, trim it, expand it, insert elements, remove elements, and so on. Such things are possible in C, but you must code the function yourself.

One of those other programming language features that intrigued me is the ability to slice a chunk of data from an array. Specifically, a question asked in an online forum was how to remove a single element from an array.

In C, you can easily replace an element in an array: a[5] = 8 (or whatever). But to pluck out an element requires writing a function in C. This task isn’t that difficult, but the big issue is what happens after the element is removed?

Before I thought further on the topic, I wrote a function, remove_element(). It zaps a given element in an array and then moves any subsequent elements over a notch to fill in the “empty” element’s position. Because the C language doesn’t allow a programmer to re-size an array, I fill in the final array element with a zero.

Here is code showing my remove_element() function:

2024_09_07-Lesson.c

#include <stdio.h>

void remove_element(int array[], int size, int element)
{
    int x;

    for( x=element; x<size-1; x++ )
        array[x] = array[x+1];
    array[x] = 0;
}

int main()
{
    int alpha[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int x;

    puts("Original array:");
    for(x=0; x<10; x++)
       printf("%2d ",alpha[x]);
    putchar('\n');

    remove_element(alpha,10,4);

    puts("After removing the 5th element:");
    for(x=0; x<10; x++)
       printf("%2d ",alpha[x]);
    putchar('\n');

    return 0;
}

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:

remove_element(alpha,10,4);

Within the remove_element() function, a for loop processes elements from the passed argument (the element to remove) to the end of the array, argument size (the array’s size). For each element, the next element is copied to the preceding position, scootching all elements over a notch.

After the loop is complete, the value zero is assigned to the final element in the array. Again, you can’t resize an array in C, so I figured that putting a zero there is better than retaining the original final element, which is now the value stored in the next-to-final element.

Here’s output from a sample run:

Original array:
 1  2  3  4  5  6  7  8  9 10 
After removing the 5th element:
 1  2  3  4  6  7  8  9 10  0 

The number of array manipulation functions (“methods”, ugh) available in other languages is abundant. You can pluck a chunk from an array to create a new array, merge arrays, append them, and so on. These are all fun activities you can emulate in C, but lacking the capability to resize an array on-the-fly makes emulating these functions difficult.

Yes, I know that the C99 standard allows for variable length arrays (VLAs). This feature is more about assigning an array’s length than dynamically altering it as the program runs. I wrote about it in an earlier post, where I quoted Linus Torvalds on Wikipedia saying, “USING VLA’S IS ACTIVELY STUPID!”

If you want a dynamic array, allocate a buffer and use pointers.

Leave a Reply