Initialize struct fileds to zero

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vashwath@rediffmail.com

    Initialize struct fileds to zero

    Hi all,
    The following program is compiled using gcc with "-W" option. GCC is
    giving the following warning message

    initStructElem. c: In function `main':
    initStructElem. c:20: warning: missing initializer
    initStructElem. c:20: warning: (near initialization for `str1.f')

    Here is the program

    1 #include <stdio.h>
    2
    3 int main(void)
    4 {
    5 typedef struct
    6 {
    7 int i1;
    8 float f1;
    9 }str_1t;
    10
    11 typedef struct
    12 {
    13 int i;
    14 float f;
    15 char c;
    16 int *pi;
    17 str_1t s2;
    18 }str_t;
    19
    20 str_t str1={0};
    21
    22 printf("str1.i= %d\t str1.f=%f\t str1.c=%d\t
    str1.pi=%p\n",s tr1.i,str1 .f,str1.c,str1. pi);
    23 printf("str1.s2 .i1=%d\n",str1. s2.i1);
    24 return 0;
    25 }
    Please let me know why gcc is complaining.
    Is this not a standard of way of intializing structure fields?

  • usr.root@gmail.com

    #2
    Re: Initialize struct fileds to zero


    vashwath@rediff mail.com 写道:
    [color=blue]
    > Hi all,
    > The following program is compiled using gcc with "-W" option. GCC is
    > giving the following warning message
    >
    > initStructElem. c: In function `main':
    > initStructElem. c:20: warning: missing initializer
    > initStructElem. c:20: warning: (near initialization for `str1.f')
    >
    > Here is the program
    >
    > 1 #include <stdio.h>
    > 2
    > 3 int main(void)
    > 4 {
    > 5 typedef struct
    > 6 {
    > 7 int i1;
    > 8 float f1;
    > 9 }str_1t;
    > 10
    > 11 typedef struct
    > 12 {
    > 13 int i;
    > 14 float f;
    > 15 char c;
    > 16 int *pi;
    > 17 str_1t s2;
    > 18 }str_t;
    > 19
    > 20 str_t str1={0};
    > 21
    > 22 printf("str1.i= %d\t str1.f=%f\t str1.c=%d\t
    > str1.pi=%p\n",s tr1.i,str1 .f,str1.c,str1. pi);
    > 23 printf("str1.s2 .i1=%d\n",str1. s2.i1);
    > 24 return 0;
    > 25 }
    > Please let me know why gcc is complaining.
    > Is this not a standard of way of intializing structure fields?[/color]

    the problem is the "int *pi;",it's a pointer ,you must intializing it
    before use it !

    Comment

    • S.Tobias

      #3
      Re: Initialize struct fileds to zero

      vashwath@rediff mail.com wrote:[color=blue]
      > Hi all,
      > The following program is compiled using gcc with "-W" option. GCC is
      > giving the following warning message
      >
      > initStructElem. c: In function `main':
      > initStructElem. c:20: warning: missing initializer
      > initStructElem. c:20: warning: (near initialization for `str1.f')
      >
      > Here is the program
      >[/color]
      [snip][color=blue]
      > 5 typedef struct
      > 6 {
      > 7 int i1;
      > 8 float f1;
      > 9 }str_1t;
      > 10
      > 11 typedef struct
      > 12 {
      > 13 int i;
      > 14 float f;
      > 15 char c;
      > 16 int *pi;
      > 17 str_1t s2;
      > 18 }str_t;
      > 19
      > 20 str_t str1={0};[/color]
      [snip]
      [color=blue]
      > Please let me know why gcc is complaining.[/color]

      Because you've invoked it in supercalifragil ipedantic mode.
      It wants you to give explicit initializers for all struct
      members:
      str_t str1={0,0,0,0,0 ,0};
      (ie.: str1.i, str1.f, str1.c, str1.pi, str1.s2.i1, str1.s2.f1).
      Have a look in the compiler docs what `-W' option does.
      [color=blue]
      > Is this not a standard of way of intializing structure fields?[/color]

      str_t str1={0};
      is perfectly stardard and defined.

      [OT] Perhaps you want `-Wall -pedantic' options instead of `-W'.

      --
      Stan Tobias
      mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

      Comment

      • vashwath@rediffmail.com

        #4
        Re: Initialize struct fileds to zero

        Thanks for the reply Tobias.[color=blue][color=green]
        >>Perhaps you want `-Wall -pedantic' options instead of `-W'.[/color][/color]
        I have to compile using -W option because this is one of the
        requirement from my client. Is there other way of intializing all the
        fields of big structures to 0?

        Comment

        • Flash Gordon

          #5
          Re: Initialize struct fileds to zero

          usr.root@gmail. com wrote:[color=blue]
          > vashwath@rediff mail.com 写道:
          >
          >[color=green]
          >>Hi all,
          >>The following program is compiled using gcc with "-W" option. GCC is
          >>giving the following warning message
          >>
          >>initStructEle m.c: In function `main':
          >>initStructEle m.c:20: warning: missing initializer
          >>initStructEle m.c:20: warning: (near initialization for `str1.f')
          >>
          >>Here is the program
          >>
          >> 1 #include <stdio.h>
          >> 2
          >> 3 int main(void)
          >> 4 {
          >> 5 typedef struct
          >> 6 {
          >> 7 int i1;
          >> 8 float f1;
          >> 9 }str_1t;
          >> 10
          >> 11 typedef struct
          >> 12 {
          >> 13 int i;
          >> 14 float f;
          >> 15 char c;
          >> 16 int *pi;
          >> 17 str_1t s2;
          >> 18 }str_t;
          >> 19
          >> 20 str_t str1={0};[/color][/color]

          The above line is correct as far as the standard is concerned.
          Personally I would be inclined to just disable that warning in gcc, but
          the specifics of how to do that are not on topic here.
          [color=blue][color=green]
          >> 21
          >> 22 printf("str1.i= %d\t str1.f=%f\t str1.c=%d\t
          >>str1.pi=%p\n" ,str1.i,str1 .f,str1.c,str1. pi);[/color][/color]

          This is one instance when a cast is required since %p requires a pointer
          to void but str1.pi is a pointer to int.
          printf("str1.i= %d\t str1.f=%f\t str1.c=%d\t str1.pi=%p\n",
          str1.i,str1.f,s tr1.c,(void*)st r1.pi);
          [color=blue][color=green]
          >> 23 printf("str1.s2 .i1=%d\n",str1. s2.i1);
          >> 24 return 0;
          >> 25 }
          >>Please let me know why gcc is complaining.
          >>Is this not a standard of way of intializing structure fields?[/color]
          >
          > the problem is the "int *pi;",it's a pointer ,you must intializing it
          > before use it ![/color]

          It *has* been initialised to a null pointer by the line[color=blue][color=green]
          >> 20 str_t str1={0};[/color][/color]
          Since it is only the value of the pointer being printed, not what it
          points to, this is fine.
          --
          Flash Gordon
          Living in interesting times.
          Although my email address says spam, it is real and I read it.

          Comment

          • vashwath@rediffmail.com

            #6
            Re: Initialize struct fileds to zero

            Hi all,
            Is there any other method of initializing structure for which GCC will
            not give any warnings when compiled with -W option?

            Comment

            • S.Tobias

              #7
              Re: Initialize struct fileds to zero

              vashwath@rediff mail.com wrote:[color=blue]
              > Thanks for the reply Tobias.[color=green][color=darkred]
              >>>Perhaps you want `-Wall -pedantic' options instead of `-W'.[/color][/color]
              > I have to compile using -W option because this is one of the
              > requirement from my client.[/color]

              Why don't you ask your client how he wants his structs initialized then?
              [color=blue]
              >Is there other way of intializing all the
              > fields of big structures to 0?[/color]

              A macro per each struct might help you:

              struct bar { /*...*/ };
              #define bar_zero_initia lizer {0, 0 /*...*/}

              struct foo_with_bar { /*...*/ struct bar b; /*...*/ };
              #define foo_with_bar_ze ro_initializer \
              {0, /*...*/ bar_zero_initia lizer, 0, /*...*/}

              const struct foo_with_bar cfwb = foo_with_bar_ze ro_initializer;

              --
              Stan Tobias
              mailx `echo siXtY@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`

              Comment

              • Stan Milam

                #8
                Re: Initialize struct fileds to zero

                vashwath@rediff mail.com wrote:
                [color=blue]
                > Thanks for the reply Tobias.
                >[color=green][color=darkred]
                >>>Perhaps you want `-Wall -pedantic' options instead of `-W'.[/color][/color]
                >
                > I have to compile using -W option because this is one of the
                > requirement from my client. Is there other way of intializing all the
                > fields of big structures to 0?
                >[/color]
                man memset.

                Comment

                • Keith Thompson

                  #9
                  Re: Initialize struct fileds to zero

                  Stan Milam <stmilam@swbell .net> writes:[color=blue]
                  > vashwath@rediff mail.com wrote:
                  >[color=green]
                  >> Thanks for the reply Tobias.
                  >>[color=darkred]
                  >>>>Perhaps you want `-Wall -pedantic' options instead of `-W'.[/color]
                  >> I have to compile using -W option because this is one of the
                  >> requirement from my client. Is there other way of intializing all the
                  >> fields of big structures to 0?
                  >>[/color]
                  > man memset.[/color]

                  memset() will not portably set pointers or floating-point objects to
                  zero (NULL or 0.0, respectively). It will happen to do so on many
                  systems, but you shouldn't depend on it.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • vashwath@rediffmail.com

                    #10
                    Re: Initialize struct fileds to zero

                    >>>I have to compile using -W option because this is one of the[color=blue][color=green][color=darkred]
                    >>> requirement from my client. Is there other way of intializing all the
                    >>> fields of big structures to 0?[/color][/color][/color]

                    [color=blue][color=green]
                    >> man memset.[/color][/color]


                    [color=blue]
                    >memset() will not portably set pointers or floating-point objects to
                    >zero (NULL or 0.0, respectively). It will happen to do so on many
                    >systems, but you shouldn't depend on it.[/color]

                    Are there any other standard,Portab le methods of doing this?

                    Comment

                    • Keith Thompson

                      #11
                      Re: Initialize struct fileds to zero

                      vashwath@rediff mail.com writes:[color=blue][color=green][color=darkred]
                      >>>>I have to compile using -W option because this is one of the
                      >>>> requirement from my client. Is there other way of intializing all the
                      >>>> fields of big structures to 0?[/color][/color]
                      >[color=green][color=darkred]
                      >>> man memset.[/color][/color]
                      >[color=green]
                      >>memset() will not portably set pointers or floating-point objects to
                      >>zero (NULL or 0.0, respectively). It will happen to do so on many
                      >>systems, but you shouldn't depend on it.[/color]
                      >
                      > Are there any other standard,Portab le methods of doing this?[/color]

                      Yes. You can either provide explicit initial values for all the
                      members, or you can use { 0 }. The latter is legal in both C90 and
                      C99; all unspecified members are initialized the same as objects with
                      static storage duration (i.e., set to 0, or NULL, or 0.0, or whatever
                      as appropriate for the type).

                      If you want to know how to use { 0 } without getting a spurious
                      warning from gcc, I suggest asking in gnu.gcc.help.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Keith Thompson

                        #12
                        Re: Initialize struct fileds to zero

                        vashwath@rediff mail.com writes:[color=blue]
                        > Thanks for the reply Tobias.[color=green][color=darkred]
                        >>>Perhaps you want `-Wall -pedantic' options instead of `-W'.[/color][/color]
                        > I have to compile using -W option because this is one of the
                        > requirement from my client. Is there other way of intializing all the
                        > fields of big structures to 0?[/color]

                        I've looked into this a bit more, reading the gcc documentation.
                        Strictly speaking this is off-topic, but I think it's worth mentioning
                        because people are often advised here to use gcc's "-W" option.

                        The following `-W...' options are not implied by `-Wall'. Some of
                        them warn about constructions that users generally do not consider
                        questionable, but which occasionally you might wish to check for;
                        others warn about constructions that are necessary or hard to avoid in
                        some cases, and there is no simple way to modify the code to suppress
                        the warning.

                        `-Wextra'
                        (This option used to be called `-W'. The older name is still
                        supported, but the newer name is more descriptive.) Print extra
                        warning messages for these events:

                        [...]

                        * An aggregate has an initializer which does not initialize all
                        members. This warning can be independently controlled by
                        `-Wmissing-field-initializers'.

                        The original problem was something like this:

                        struct s {
                        int a;
                        void *b;
                        };
                        struct s obj = { 0 };

                        The warning appears because there's no explicit initializer for b, but
                        the code is perfectly valid. In fact, it's arguably better than
                        providing explicit initializations for all the members, since it
                        doesn't have to be changed when the struct definition is changed.

                        If you're required to use gcc's "-W" option, and your code is not
                        allowed to produce any warnings, then you're effectively programming
                        in a restricted subset of C.

                        Personally, I tend to use "-W -Wall", but I feel free to ignore
                        warnings that don't make sense. If you don't have that freedom,
                        that's a matter between you and your client.

                        The simplest solution is probably to provide explicit initializers
                        for all the members; in the sample above:
                        struct s obj = { 0, NULL };

                        IMHO, gcc could be improved by treating { 0 } as a special case, not
                        affected by "-Wmissing-field-initializers".

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        Working...