problem initializing a structure to zero

Andrew Haley aph@redhat.com
Fri Apr 11 18:43:00 GMT 2008


Jelle Geerts wrote:
> Hello,
> 
> When compiling the enclosed source code (see attachment), I get the
> following warnings from GCC:
> --------
> test.c: In function `main':
> test.c:15: warning: missing initializer
> test.c:15: warning: (near initialization for `my_struct.var1')
> --------
> 
> When trying the suggestion (something I read on
> http://bytes.com/forum/thread212987.html) of adding braces around
> `{0}' to make it `{{0}}', GCC gives me the following warnings:
> --------
> test.c: In function `main':
> test.c:15: warning: braces around scalar initializer
> test.c:15: warning: (near initialization for `my_struct.var0')
> test.c:15: warning: missing initializer
> test.c:15: warning: (near initialization for `my_struct.var1')
> --------
> 
> What is the correct way to easily initialize a struct and all it's
> members (recursively perhaps?) to zero?

 struct some_struct my_struct = {0}

is okay.  

> I read that `= {0}' is valid code and shouldn't need to be changed.

It is, but it is probably doubtful style.  

"6.7.8 Initialization

"21.  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."

Andrew.



More information about the Gcc-help mailing list