Missing initializer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Old Wolf

    #1

    Missing initializer

    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).

  • Mike Wahler

    #2
    Re: Missing initializer

    "Old Wolf" <oldwolf@inspir e.net.nz> wrote in message
    news:1104886521 .592405.232290@ c13g2000cwb.goo glegroups.com.. .[color=blue]
    > 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[/color]


    Not that I can see.
    [color=blue]
    > or is this just
    > paranoia from the compiler. (My intention was that all
    > members of 's' be zero-initialized).[/color]

    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


    Comment

    • Derrick Coetzee

      #3
      Re: [OT] Missing initializer

      Old Wolf wrote:[color=blue]
      > 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.[/color]

      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.

      Comment

      • Micah Cowan

        #4
        Re: Missing initializer

        Old Wolf wrote:
        [color=blue]
        > 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).
        >[/color]

        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.

        Comment

        Working...