I have a nested struct in C99 (I'm using GCC 4.8.3 with -std=gnu99 -Wall
).
struct X
{
struct
{
int p;
int q;
}
a;
struct
{
int m;
int n;
}
b;
int c, d, e;
};
I want to define a "default value" for it which is all-zeroes. One way would be to explicitly specify the value of each and every field, but I'd like to use the shortcut { 0 }
as a default value. This is known as the "universal zero-initializer" - assigning it to a variable will zero all fields of that variable.
However, if I try this:
struct X x = { 0 };
I get warning: missing braces around initializer
, then further warnings about missing initializers for fields of X.
Generally, zeroing x
is not a problem. I am aware of other options such as memset()
and using automatic initialization of a static variable to all-zeroes. This question is about the universal zero-initializer, and why it generates warnings unexpectedly.
Why does the above generate warnings, when it seems like it should be fine?