Initializing Only Part of an Array

I’m continually amazed when I discover some aspect of the C programming language that I’ve not encountered before. This time, the trick is how to initialize individual array elements.

The standard array declaration is a statement with the data type, array name, and a part of brackets:

int numbers[6];

The brackets can show the element count, such as six above, or they can be empty. If empty, the elements are assigned directly, as shown in this code:

2023_12_09-Lesson-a.c

#include <stdio.h>

int main()
{
    int numbers[] = { 10, 20, 30, 40, 50, 60 };
    int x;

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

    return(0);
}

The compiler automatically allocates array storage for the six integers shown above. You could also declare the array and assign values separately. But the new thing I learned is that you need not assign all the values when declaring an array.

For example, say the array has storage for six elements but your code wants to initialize only the fourth and fifth elements. You assign them after declaring the array like this:

int numbers[6];

numbers[3] = 40;
numbers[4] = 50;

Or you can use the format shown in this code:

2023_12_09-Lesson-b.c

#include <stdio.h>

int main()
{
    int numbers[6] = {
        [3] = 40,
        [4] = 50
    };
    int x;

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

    return(0);
}

The format above is the thing I’ve not seen before: brackets with the element number and an assignment. Only elements three and four are assigned values and just by using the brackets and the element number. The remainder of the array’s elements are uninitialized, which you can see in a sample run:

0
0
0
40
50
0

The uninitialized elements may show zero or whatever garbage already exists in memory. Still, this format is something I’ve not seen before:

int numbers[6] = {
    [3] = 40,
    [4] = 50
};

I’m unsure of how I would use this type of initialization, though it’s legitimate and available. Yet another weird thing about C that crops up every so often.

One thought on “Initializing Only Part of an Array

  1. During Dawid Zalewskiʼs talk¹ on »Programming in Modern C with a Sneak Peek into C23«, as presented during ACCU 2023 in Bristol, the following code snippet comes up:

    double numbers[] = {0, [10]=55, 89, [5]=5, 8, [1]=1, 1, 2};
    // What is the size of the numbers array? (Answer at the end of this post.)

    … all remaining array elements, whose value is not explicitly specified, are guaranteed to be zero-initialized—as per §6.7.8.21 of the ISO/IEC 9899:1999 standard, which introduced these “designated initializers”:

    »If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.«

    I donʼt usually use them as well, but after thinking for awhile about how they might be used beneficially, I came up with the following example:

    #include <stdio.h>

    #ifndef _countof
    #define _countof(arr) (sizeof(arr) / sizeof((arr)[0]))
    #endif

    int main (void)
    { /* Array is terminated by a single -1 at the end: */
      int data [5] = { [_countof(data)-1] = -1 };
      size_t i;

      for (i=0; i < _countof(data); ++i)
        fprintf (stdout, "data[%zu] == %d\n", i, data[i]);

      return (0);
    }

    … a designated initializer thereby ensuring that the last element in a statically allocated array is initialized to some “magic value”.

    Answer to the quiz from the beginning: with [10]=55, the array element at index 10 is set to the value 55, after which the following element is set to the value 89, thereby forcing the compiler to create an array with 12 elements.

    ¹ Presentation slides of this talk can be found on his website: https://www.dev0notes.com/speaking

Leave a Reply