Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Static Array Initialization - how verbose do I need to be?

Tags:

c++

arrays

c

To initialize an int array with all zeros, do I need to use:

int foo[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

Or, will this work:

int foo[10] = {0};
like image 599
jgottula Avatar asked Aug 29 '09 21:08

jgottula


1 Answers

In C/C++ if you initialize just the first element of an array of known size with a value, the remainder will be zero-filled, so:

int foo[10] = {0};

will do exactly what you want.

This also works for structs:

struct bar {
    int x;
    int y;
    char c;
} myBar = {0};

will initialize all members to 0.

The standard (C99 - 6.7.8/12 - Initialization) says this:

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.

In C the grammar requires that there be at least one 'assignment-expression' inside the braces. An 'assignment-expression' can be many things from a constant or identifier up through much more complex expressions. However, an empty string doesn't qualify as an 'assignment-expression', so there has to be something between the braces.

In C++, the grammar specifically allows the '{ }' initializer, so the following would also zero-initialize the array:

int foo[10] = {};

It's probably also worth noting that in C++ the entries that don't have a specific initializer value in the initialize list will be 'value-initialized' or 'default-initialized' which might be different than being zero-initialized depending on what the constructors for the variable type are and whether the compiler is following the C++98 standard or the C++03 standard (this is probably the only difference of any significance between C++98 and C++03). The whole situation with value vs. default initialization is rather complicated, so if you're interested see this answer: Do the parentheses after the type name make a difference with new?.

Fortunately, the difference doesn't seem to cause much trouble in practice, although if you run into it, it would probably cause some head scratching for a while when trying to figure out what the behavior really should be. I usually don't really think much about it - it makes my head hurt.

like image 162
Michael Burr Avatar answered Sep 28 '22 09:09

Michael Burr