Yet Another Way to Cap an Array

You don’t know how many items the array might store, so you guess. Then the program fetches only n values, all in the range of –X through 0 to +X. How do you know when the valid values stop?

As a review, arrays in the C language are simply a chunk of memory. The compiler knows the variable type for the chunk, so it treats the elements as properly-sized units, but that’s it!

In the following code, an array capable of holding 5 integers is declared. Values are assigned to the first three elements.

#include <stdio.h>

int main()
{
    int a[5],x;

    a[0] = -64;
    a[1] = 0;
    a[2] = 64;

    for(x=0;x<5;x++)
        printf("a[%d] = %d\n",x,a[x]);

    return(0);
}

Here’s sample output:

a[0] = -64
a[1] = 0
a[2] = 64
a[3] = 0
a[4] = 1508375224

Because elements a[3] and a[4] are uninitialized, the values displayed are considered garbage. It’s absolutely random that I see zero for a[3], for example. And your output will be different as well.

So how could you craft an array that holds all signed values for an integer range, but yet also makes the final element known in a dynamic manner?

Sure, you can create a variable that keeps track, and that’s how most C programmers solve the problem. That trick doesn’t work all the time, so as I wrote in last week’s Lesson, the answer is to cheat.

You transform the array into a packet. That’s sort of a cross between a structure and an array. It’s formatted data.

In this case the format involves two chunks: Element 0 of the array contains the number of valid elements in the array, not counting itself. Those valid elements start at element 1 and go up through the value of element 0. Figure 1 illustrates this concept.

Figure 1. Formatted data in an array.

Figure 1. Formatted data in an array.

The following code demonstrates a formatted array of 100 elements. It’s filled with random values up to 100 elements. The total number of elements is held in the first element in the array, n[0].

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int n[100];
    int x;

    srand((unsigned)time(NULL));

    /* Set maximum elements, 1 to 100 */
    n[0] = rand() % 99 + 1;

    /* Fill the array to `max` */
    for(x=1;x<=n[0];x++)
        n[x] = 100 - (rand() % 200);

    /* display the array data */
    printf("The array contains %d elements.\n",n[0]);
    for(x=1;x<=n[0];x++)
        printf("%d\t",n[x]);
    putchar('\n');
    
    return(0);
}

Line 13 sets the number of array elements to a value between 1 and 99. The value is stored in element n[0], the first element in the array.

Lines 16 and 17 fill n[0] elements of the array with random values. The for loop must use the evaluation x<=n[0] to ensure that the last valid array element is filled. This evaluation happens again at Line 21.

The equation at Line 17 allows for both positive and negative values, just to make the output interesting.

Lines 21 through 23 display the results.

Here’s sample output:

The array contains 9 elements.
-60	-56	15	12	46	27	60	-99	10

The computer kingdom is filled with plenty of examples of this data storage. Mostly this type of formatted data is held in structures. My point with using an array is simply to show how an array could be presented to hold a dynamic number of elements without requiring too much program overhead.