initialize structs

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

    initialize structs

    Hi,

    I have a doubt regarding structus.. I have to following:

    typedef struct STATS
    {
    int src;
    int dest;
    int total;
    int drop;
    } STATS;

    And then, the following:

    STATS e_STATS[10];

    I want my e_STATS[10] initialize to 0... however, i tried several ways
    and i cannot make it work..

    tnx,
    Fernando

  • FBM

    #2
    Re: initialize structs

    Got it.. e_STATS is an int *... so, the one way is to do 'memset'...
    sorry, i didnt think it through..

    tnx,
    Fernando

    Comment

    • Michael Brennan

      #3
      Re: initialize structs

      FBM wrote:[color=blue]
      > Hi,
      >
      > I have a doubt regarding structus.. I have to following:
      >
      > typedef struct STATS
      > {
      > int src;
      > int dest;
      > int total;
      > int drop;
      > } STATS;
      >
      > And then, the following:
      >
      > STATS e_STATS[10];
      >
      > I want my e_STATS[10] initialize to 0... however, i tried several ways
      > and i cannot make it work..
      >[/color]

      Change the declaration to

      STATS e_STATS[10] = { 0 };

      and it will be initialized to 0.

      /Michael

      Comment

      • FBM

        #4
        Re: initialize structs

        Thanks.. it's working, but i get this nasty warning..

        STATS mySTATS[10] = { 0 };

        gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
        .../main.c: In function 'main':
        .../main.c:44: warning: missing braces around initializer
        .../main.c:44: warning: (near initialization for 'mySTATS[0]')
        Finished building: ../main.c

        Any idea why?

        tnx,
        Fernando

        Comment

        • pete

          #5
          Re: initialize structs

          FBM wrote:[color=blue]
          >
          > Thanks.. it's working, but i get this nasty warning..
          >
          > STATS mySTATS[10] = { 0 };
          >
          > gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
          > ../main.c: In function 'main':
          > ../main.c:44: warning: missing braces around initializer
          > ../main.c:44: warning: (near initialization for 'mySTATS[0]')
          > Finished building: ../main.c
          >
          > Any idea why?[/color]

          Probably has something to do with the code
          that's on or near line 44.

          --
          pete

          Comment

          • Eric Sosman

            #6
            Re: initialize structs



            FBM wrote On 05/01/06 10:48,:[color=blue]
            > Hi,
            >
            > I have a doubt regarding structus.. I have to following:
            >
            > typedef struct STATS
            > {
            > int src;
            > int dest;
            > int total;
            > int drop;
            > } STATS;
            >
            > And then, the following:
            >
            > STATS e_STATS[10];
            >
            > I want my e_STATS[10] initialize to 0... however, i tried several ways
            > and i cannot make it work..[/color]

            If e_STATS has "static storage duration" (that is, if
            is defined outside any function or if it is defined with the
            `static' keyword), it will be initialized to zero. Since you
            say it isn't zeroed, I guess you are defining it inside a
            function and it is an "automatic" variable; these are not
            initialized unless you initialize them. The easiest way is

            STATS e_STATS[10] = { 0 };

            --
            Eric.Sosman@sun .com

            Comment

            • Flash Gordon

              #7
              Re: initialize structs

              FBM wrote:[color=blue]
              > Got it.. e_STATS is an int *... so, the one way is to do 'memset'...
              > sorry, i didnt think it through..[/color]

              Please provide context, even when replying to yourself. There is no
              guarantee that everyone has seen (or ever will see) the message you are
              replying to. Even if they have seen it they may not remember it. Google
              is not Usenet just a poor excuse for an interface to it. See
              http://clc-wiki.net/wiki/Intro_to_clc specifically the bit about Google.

              Secondly, arrays are *not* pointers, and in particular an array of
              structs is *not* a pointer to int! It is a *very* long way from it. See
              the comp.lang.c FAQ http://c-faq.com/ specifically question 6.9, but the
              rest of section 6 and the entire FAQ as well.
              --
              Flash Gordon, living in interesting times.
              Web site - http://home.flash-gordon.me.uk/
              comp.lang.c posting guidelines and intro:

              Comment

              • Amit  Limaye

                #8
                Re: initialize structs

                just one thing only 0 initialization is guranteed
                dont try nething else it is not :)
                i got bit by this once so just a warning

                -SIGTERM
                amit

                Comment

                • Ben Pfaff

                  #9
                  Re: initialize structs

                  "Amit Limaye" <amit.limaye@gm ail.com> writes:
                  [color=blue]
                  > just one thing only 0 initialization is guranteed
                  > dont try nething else it is not :)
                  > i got bit by this once so just a warning[/color]

                  It sounds like this may have been a bug in your implementation,
                  although you didn't give enough details to tell.
                  --
                  int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                  \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                  );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                  );}return 0;}

                  Comment

                  • Barry Schwarz

                    #10
                    Re: initialize structs

                    On 1 May 2006 08:32:47 -0700, "FBM" <fbarsoba@gmail .com> wrote:
                    [color=blue]
                    >Thanks.. it's working, but i get this nasty warning..
                    >
                    >STATS mySTATS[10] = { 0 };
                    >
                    >gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
                    >../main.c: In function 'main':
                    >../main.c:44: warning: missing braces around initializer
                    >../main.c:44: warning: (near initialization for 'mySTATS[0]')
                    >Finished building: ../main.c
                    >[/color]
                    You probably need {{0}}. The outer set of braces denotes
                    initialization data. The inner set indicates it applies to the first
                    element of the array (mySTATS[0]). Since there are less initializers
                    than members of mySTATS[0], the first member is initialized explicitly
                    and the remaining members are set to the appropriate form of 0
                    implicitly. Since there are less array initializers than elements of
                    the array, the remaining array elements are initialized to 0
                    implicitly.

                    Many implementations would accept your code and I don't know if the
                    standard *requires* the second set of braces. It could be
                    over-exuberance on the part of gcc. Compilers are allowed to generate
                    "extraneous " diagnostics for anything they don't like as long as they
                    generate the correct code. (I'm waiting for one that complains the
                    indenting style is unreadable for some of the code posted here.)


                    Remove del for email

                    Comment

                    • Flash Gordon

                      #11
                      Re: initialize structs

                      Barry Schwarz wrote:[color=blue]
                      > On 1 May 2006 08:32:47 -0700, "FBM" <fbarsoba@gmail .com> wrote:
                      >[color=green]
                      >> Thanks.. it's working, but i get this nasty warning..
                      >>
                      >> STATS mySTATS[10] = { 0 };
                      >>
                      >> gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
                      >> ../main.c: In function 'main':
                      >> ../main.c:44: warning: missing braces around initializer
                      >> ../main.c:44: warning: (near initialization for 'mySTATS[0]')
                      >> Finished building: ../main.c
                      >>[/color]
                      > You probably need {{0}}. The outer set of braces denotes
                      > initialization data. The inner set indicates it applies to the first
                      > element of the array (mySTATS[0]). Since there are less initializers
                      > than members of mySTATS[0], the first member is initialized explicitly
                      > and the remaining members are set to the appropriate form of 0
                      > implicitly. Since there are less array initializers than elements of
                      > the array, the remaining array elements are initialized to 0
                      > implicitly.
                      >
                      > Many implementations would accept your code and I don't know if the
                      > standard *requires* the second set of braces.[/color]

                      The standard definitely does *not* require the second set of braces.
                      [color=blue]
                      > It could be
                      > over-exuberance on the part of gcc. Compilers are allowed to generate
                      > "extraneous " diagnostics for anything they don't like as long as they
                      > generate the correct code. (I'm waiting for one that complains the
                      > indenting style is unreadable for some of the code posted here.)[/color]

                      The gcc warning is often useful IMHO but, in this case, it isn't. It
                      would be nice if they had an exception for the {0} initialiser.
                      --
                      Flash Gordon, living in interesting times.
                      Web site - http://home.flash-gordon.me.uk/
                      comp.lang.c posting guidelines and intro:

                      Comment

                      Working...