Initializing an Array

graphic
Like all variables in C, arrays are declared uninitialized. They contain whatever junk already exists in memory. Initializing an array is therefore important. It can be done in one of three ways: as it’s declared, in a loop, or as it’s used in the code.

Declaring an array involves assigning it a data type, name, and size:

float market_close[5];

Array market_close is composed of 5 float values. This declaration merely sets aside storage for the values; nothing is initialized.

int scores[] = { 1075, 1050, 975, 850, 600 };

Integer array scores contains 5 elements, defined as the array is declared. The number 5 isn’t required in the brackets because the compiler is smart enough to figure out that 5 elements are declared.

int scores[5] = { 0 };

In this format, integer array scores contains five elements, all zero. The { 0 } initialization applies to all five elements. The following code shows how this trick works for a floating-point array.

2020_08_22-Lesson-a.c

#include <stdio.h>

int main()
{
    const int size = 10;
    float v[size] = { 0.0 };

    puts("Array Dump");
    for(int x = 0; x<size; x++ )
        printf("%2d: %f\n",x+1, v[x] );

    return(0);
}

Array v is declared at Line 6 and initialized to size (10) float values. The dump at Lines 9 and 10 outputs the array’s values:

Array Dump
 1: 0.000000
 2: 0.000000
 3: 0.000000
 4: 0.000000
 5: 0.000000
 6: 0.000000
 7: 0.000000
 8: 0.000000
 9: 0.000000
10: 0.000000

This initialization trick works only for zero. if you initialize the array at Line 6 with this statement:

float v[size] = { 1.0 };

The output looks like this:

Array Dump
 1: 1.000000
 2: 0.000000
 3: 0.000000
 4: 0.000000
 5: 0.000000
 6: 0.000000
 7: 0.000000
 8: 0.000000
 9: 0.000000
10: 0.000000

The first (zeroth) element is 1.0, but the rest are zero. I really wish all the elements were initialized to the same value, but that’s not how it works.

If it’s your desire to initialize an array to a specific value, run a loop that sets all the array’s values. This is the approach I use in my code:

2020_08_22-Lesson-b.c

#include <stdio.h>

int main()
{
    const int size = 10;
    int v[size];

    /* initialize array */
    for(int x = 0; x<size; x++ )
        v[x] = -1;

    /* output array */
    puts("Array Dump");
    for(int x = 0; x<size; x++ )
        printf("%2d: %d\n",x+1, v[x] );

    return(0);
}

The for loop at Line 9 initializes all values in the array to -1. That’s it. Output:

Array Dump
 1: -1
 2: -1
 3: -1
 4: -1
 5: -1
 6: -1
 7: -1
 8: -1
 9: -1
10: -1

I’ve read online that the gcc compiler offered a non-standard initialization alternative. It uses an ellipses in the square brackets to assign a set value to a range of elements. The format looks something like this:

float v[10] = { [1 . . . 10] 1.0 };

The brackets in the assignment specify a range from 1 to 10 elements (or it should be 0 to 9?). These elements are given the value 1.0. On my systems, the gcc compiler doesn’t support this trick. Even so, if it works, it’s non-standard and I wouldn’t recommend it.

The = { 0 } initialization format is available for array initialization, but I still prefer to use a loop to set clean all the elements’ values. It’s not that I don’t trust the compiler but . . . No, wait. I don’t trust the compiler. Yeah, just initialize your arrays. It’s a good thing.

2 thoughts on “Initializing an Array

  1. I tried the “non-standard for” as you had it and I got an error but once I changed the starting and stopping index to match C’s, 0 Based array syntax it compiled fine under gcc (-v 8.2.0-5). You can also put a equal sign in .

    float v[10] = { [0 … 9] 1.0 };
    float v[10] = { [0 … 9] = 1.0};

  2. I have gcc 7.4.0. It works in the second format, which I didn’t consider. The first still throws a “Missing =” warning. Thanks!

    Apparently clang supports the second format. What I did wrong was to write “. . .” instead of “…” (no spaces). Weird.

Leave a Reply