Missing initializer

Discussion in 'C Programming' started by Old Wolf, Jan 5, 2005.

  1. Old Wolf

    Old Wolf Guest

    For the following code:

    struct S
    {
    char t[5][5];
    int flags;
    };

    void func(void)
    {
    struct S s = { 0 };
    }

    I get a "warning: missing initializer" from gcc in C99 mode.
    Is there something wrong with my code or is this just
    paranoia from the compiler. (My intention was that all
    members of 's' be zero-initialized).
     
    Old Wolf, Jan 5, 2005
    #1
    1. Advertising

  2. Old Wolf

    Mike Wahler Guest

    "Old Wolf" <> wrote in message
    news:...
    > For the following code:
    >
    > struct S
    > {
    > char t[5][5];
    > int flags;
    > };
    >
    > void func(void)
    > {
    > struct S s = { 0 };
    > }
    >
    > I get a "warning: missing initializer" from gcc in C99 mode.
    > Is there something wrong with my code



    Not that I can see.

    > or is this just
    > paranoia from the compiler. (My intention was that all
    > members of 's' be zero-initialized).


    The above should do just that. Perhaps the following
    form will remove the warning:

    struct S s = {{0}};

    Or be very explicit:

    struct S s = {{0}, 0};

    BTW what does the gcc documentation say about this warning?

    -Mike
     
    Mike Wahler, Jan 5, 2005
    #2
    1. Advertising

  3. Re: [OT] Missing initializer

    Old Wolf wrote:
    > struct S
    > {
    > char t[5][5];
    > int flags;
    > };
    >
    > void func(void)
    > {
    > struct S s = { 0 };
    > }
    >
    > I get a "warning: missing initializer" from gcc in C99 mode.


    Marked OT since my post is mostly about gcc. I get:

    temp.c: In function `func':
    temp.c:9: warning: missing braces around initializer
    temp.c:9: warning: (near initialization for `s.t')
    temp.c:9: warning: unused variable `s'

    These warnings all make sense (implicit braces are a nasty source of
    potential errors and good to warn about). I don't see yours. My
    command-line is:

    gcc -std=c99 -Wall temp.c -o temp

    Perhaps you misread it? If not, perhaps you have an older version of gcc
    with a known bug? I have 3.3.4 20040623.
    --
    Derrick Coetzee
    I grant this newsgroup posting into the public domain. I disclaim all
    express or implied warranty and all liability. I am not a professional.
     
    Derrick Coetzee, Jan 5, 2005
    #3
  4. Old Wolf

    Micah Cowan Guest

    Old Wolf wrote:

    > For the following code:
    >
    > struct S
    > {
    > char t[5][5];
    > int flags;
    > };
    >
    > void func(void)
    > {
    > struct S s = { 0 };
    > }
    >
    > I get a "warning: missing initializer" from gcc in C99 mode.
    > Is there something wrong with my code or is this just
    > paranoia from the compiler. (My intention was that all
    > members of 's' be zero-initialized).
    >


    struct S s = {{{0}},0};

    seems to do the trick. The outermost braces are for the struct,
    the next set for the array of arrays, and the innermost set for
    the array of char. This also shuts up the missing brace warning
    that you would get with

    gcc --std=c00 -W -Wall -c test.c

    HTH, HAND.
     
    Micah Cowan, Jan 5, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Mashak

    missing initializer

    Roman Mashak, Jan 4, 2007, in forum: C Programming
    Replies:
    4
    Views:
    914
    Barry Schwarz
    Jan 4, 2007
  2. Pawel
    Replies:
    13
    Views:
    18,786
  3. Boon

    missing braces around initializer

    Boon, May 13, 2009, in forum: C Programming
    Replies:
    3
    Views:
    9,042
    Robert Gamble
    May 13, 2009
  4. Ben Pfaff

    Re: Missing braces around {0} initializer?!

    Ben Pfaff, Feb 8, 2010, in forum: C Programming
    Replies:
    0
    Views:
    714
    Ben Pfaff
    Feb 8, 2010
  5. Seebs
    Replies:
    27
    Views:
    3,328
    Phil Carmody
    Feb 11, 2010
Loading...

Share This Page