skipping a structure member while initializing

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

    skipping a structure member while initializing

    Hallo.
    Let's say, there is a structure
    struct struct10{
    int a1;
    int a2;
    int a3;
    int a4;
    }count[2]={
    {10,20,30,40},
    {50,60,70,80}
    };

    How can I skip a member, e.g. a2 while initializing?
    Thanks a lot!

  • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

    #2
    Re: skipping a structure member while initializing

    Mik0b0 wrote:
    Hallo.
    Let's say, there is a structure
    struct struct10{
    int a1;
    int a2;
    int a3;
    int a4;
    }count[2]={
    {10,20,30,40},
    {50,60,70,80}
    };
    >
    How can I skip a member, e.g. a2 while initializing?
    Thanks a lot!
    When a structure has a partial initialiser, the members without an
    initialiser get set to 0. You can just as easily type 0 yourself;
    there's no difference in effect.

    Comment

    • Mik0b0

      #3
      Re: skipping a structure member while initializing

      On Mar 10, 2:15 am, "Harald van Dijk" <true...@gmail. comwrote:
      Mik0b0 wrote:
      Hallo.
      Let's say, there is a structure
      struct struct10{
      int a1;
      int a2;
      int a3;
      int a4;
      }count[2]={
      {10,20,30,40},
      {50,60,70,80}
      };
      >
      How can I skip a member, e.g. a2 while initializing?
      Thanks a lot!
      >
      When a structure has a partial initialiser, the members without an
      initialiser get set to 0. You can just as easily type 0 yourself;
      there's no difference in effect.
      OK, but what can I do if I initialized a2 in every array of structures
      before?
      I mean

      for (i=0;i<2;i++)
      {
      count[i].a2=(i+1);
      };
      and after that I want to initialize all other members of all arrays?
      Thanks!

      Comment

      • Keith Thompson

        #4
        Re: skipping a structure member while initializing

        "Mik0b0" <newssw@gmail.c omwrites:
        On Mar 10, 2:15 am, "Harald van Dijk" <true...@gmail. comwrote:
        >Mik0b0 wrote:
        Hallo.
        Let's say, there is a structure
        struct struct10{
        int a1;
        int a2;
        int a3;
        int a4;
        }count[2]={
        {10,20,30,40},
        {50,60,70,80}
        };
        >>
        How can I skip a member, e.g. a2 while initializing?
        Thanks a lot!
        >>
        >When a structure has a partial initialiser, the members without an
        >initialiser get set to 0. You can just as easily type 0 yourself;
        >there's no difference in effect.
        >
        OK, but what can I do if I initialized a2 in every array of structures
        before?
        I mean
        >
        for (i=0;i<2;i++)
        {
        count[i].a2=(i+1);
        };
        and after that I want to initialize all other members of all arrays?
        You can only *initialize* an object when you declare it. If you're
        executing a for loop that assigns values to the elements of the
        arrays, then any initialization is already finished.

        C99 does provide compound literals (I've never used them, so I'm not
        going to go into any more detail), but there's no way to leave a
        "hole" in one. You can assign a compound literal to a struct object,
        but by assigning it you're overwriting the entire object.

        Can you initialize the object, setting the "skipped" members to 0, and
        *then* use a for loop to set values for those members?

        --
        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."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Matt Kowalczyk

          #5
          Re: skipping a structure member while initializing

          Mik0b0 wrote:
          Hallo.
          Let's say, there is a structure
          struct struct10{
          int a1;
          int a2;
          int a3;
          int a4;
          }count[2]={
          {10,20,30,40},
          {50,60,70,80}
          };
          >
          How can I skip a member, e.g. a2 while initializing?
          Thanks a lot!
          >
          You can try something like this,

          #include <stdio.h>

          #define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))

          struct foo {
          int a1;
          int a2;
          } foobar[10] = {
          [0] = {10, 10},
          [2] = {20, 2}
          };

          int main(void) {
          int i;

          for(i = 0; i < NUM(foobar); i++) {
          printf("%d %d\n", foobar[i].a1, foobar[i].a2);
          }
          return 0;
          }


          Output:

          10 10
          0 0
          20 2
          0 0
          0 0
          0 0
          0 0
          0 0
          0 0
          0 0

          This is supported in C99.

          -Matt

          Comment

          • Matt Kowalczyk

            #6
            Re: skipping a structure member while initializing

            Matt Kowalczyk wrote:
            Mik0b0 wrote:
            >Hallo.
            >Let's say, there is a structure
            > struct struct10{
            > int a1;
            > int a2;
            > int a3;
            > int a4;
            >}count[2]={
            > {10,20,30,40},
            > {50,60,70,80}
            > };
            >>
            >How can I skip a member, e.g. a2 while initializing?
            >Thanks a lot!
            >>
            >
            You can try something like this,
            >
            #include <stdio.h>
            >
            #define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))
            >
            struct foo {
            int a1;
            int a2;
            } foobar[10] = {
            [0] = {10, 10},
            [2] = {20, 2}
            };
            >
            int main(void) {
            int i;
            >
            for(i = 0; i < NUM(foobar); i++) {
            printf("%d %d\n", foobar[i].a1, foobar[i].a2);
            }
            return 0;
            }
            >
            >
            Output:
            >
            10 10
            0 0
            20 2
            0 0
            0 0
            0 0
            0 0
            0 0
            0 0
            0 0
            >
            This is supported in C99.
            >
            -Matt
            I read your original question again, and this may also help you,

            struct foo {
            int a1;
            int a2;
            } foobar[10] = {
            [0].a1 = 10,
            [2] = {20, 2}
            };

            The output would then look something like this,

            10 0
            0 0
            20 2
            0 0
            0 0
            0 0
            0 0
            0 0
            0 0
            0 0


            -Matt

            Comment

            • SRR

              #7
              Re: skipping a structure member while initializing

              On Mar 10, 10:00 am, Matt Kowalczyk <matt5...@comca st.netwrote:
              Mik0b0 wrote:
              Hallo.
              Let's say, there is a structure
              struct struct10{
              int a1;
              int a2;
              int a3;
              int a4;
              }count[2]={
              {10,20,30,40},
              {50,60,70,80}
              };
              >
              How can I skip a member, e.g. a2 while initializing?
              Thanks a lot!
              >
              You can try something like this,
              >
              #include <stdio.h>
              >
              #define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))
              >
              struct foo {
              int a1;
              int a2;} foobar[10] = {
              >
              [0] = {10, 10},
              [2] = {20, 2}
              >
              };
              >
              int main(void) {
              int i;
              >
              for(i = 0; i < NUM(foobar); i++) {
              printf("%d %d\n", foobar[i].a1, foobar[i].a2);
              }
              return 0;
              >
              }
              >
              Output:
              >
              10 10
              0 0
              20 2
              0 0
              0 0
              0 0
              0 0
              0 0
              0 0
              0 0
              I like the code, Its really interesting!
              >
              This is supported in C99.
              Can u tell me where is it mentioned in C99 standards?Pleas e.
              Thanks in advance for the reply.
              >
              -Matt- Hide quoted text -
              >
              - Show quoted text -

              Comment

              • santosh

                #8
                Re: skipping a structure member while initializing


                SRR wrote:
                On Mar 10, 10:00 am, Matt Kowalczyk <matt5...@comca st.netwrote:
                Mik0b0 wrote:
                Hallo.
                Let's say, there is a structure
                struct struct10{
                int a1;
                int a2;
                int a3;
                int a4;
                }count[2]={
                {10,20,30,40},
                {50,60,70,80}
                };
                How can I skip a member, e.g. a2 while initializing?
                Thanks a lot!
                You can try something like this,

                #include <stdio.h>

                #define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))

                struct foo {
                int a1;
                int a2;} foobar[10] = {

                [0] = {10, 10},
                [2] = {20, 2}

                };

                int main(void) {
                int i;

                for(i = 0; i < NUM(foobar); i++) {
                printf("%d %d\n", foobar[i].a1, foobar[i].a2);
                }
                return 0;

                }
                [ ... ]
                This is supported in C99.
                >
                Can u tell me where is it mentioned in C99 standards?Pleas e.
                Thanks in advance for the reply.
                Section 6.5.2.5 in n1124.pdf - Compound literals.

                Comment

                • CBFalconer

                  #9
                  Re: skipping a structure member while initializing

                  santosh wrote:
                  SRR wrote:
                  >
                  .... snip ...
                  >>
                  >Can u tell me where is it mentioned in C99 standards?Pleas e.
                  >Thanks in advance for the reply.
                  >
                  Section 6.5.2.5 in n1124.pdf - Compound literals.
                  Your name change to 'u' doesn't appear in the headers.

                  --
                  <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                  <http://www.securityfoc us.com/columnists/423>

                  "A man who is right every time is not likely to do very much."
                  -- Francis Crick, co-discover of DNA
                  "There is nothing more amazing than stupidity in action."
                  -- Thomas Matthews



                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • Rajesh S R

                    #10
                    Re: skipping a structure member while initializing

                    On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comca st.netwrote:
                    Matt Kowalczyk wrote:
                    Mik0b0 wrote:
                    Hallo.
                    Let's say, there is a structure
                    struct struct10{
                    int a1;
                    int a2;
                    int a3;
                    int a4;
                    }count[2]={
                    {10,20,30,40},
                    {50,60,70,80}
                    };
                    >
                    How can I skip a member, e.g. a2 while initializing?
                    Thanks a lot!
                    >
                    You can try something like this,
                    >
                    #include <stdio.h>
                    >
                    #define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))
                    >
                    struct foo {
                    int a1;
                    int a2;
                    } foobar[10] = {
                    [0] = {10, 10},
                    [2] = {20, 2}
                    };
                    >
                    int main(void) {
                    int i;
                    >
                    for(i = 0; i < NUM(foobar); i++) {
                    printf("%d %d\n", foobar[i].a1, foobar[i].a2);
                    }
                    return 0;
                    }
                    >
                    Output:
                    >
                    10 10
                    0 0
                    20 2
                    0 0
                    0 0
                    0 0
                    0 0
                    0 0
                    0 0
                    0 0
                    >
                    This is supported in C99.
                    >
                    -Matt
                    >
                    I read your original question again, and this may also help you,
                    >
                    struct foo {
                    int a1;
                    int a2;} foobar[10] = {
                    >
                    [0].a1 = 10,
                    [2] = {20, 2}
                    >
                    };
                    >
                    The output would then look something like this,
                    >
                    10 0
                    0 0
                    20 2
                    0 0
                    0 0
                    0 0
                    0 0
                    0 0
                    0 0
                    0 0
                    >
                    -Matt- Hide quoted text -
                    >
                    - Show quoted text -
                    I am not able to understand how the above initialisation is related
                    with Compound literals though I read about them from Ansi Standards.
                    Please explain.
                    Thanks in advance for the reply.

                    Comment

                    • Chris Torek

                      #11
                      Re: skipping a structure member while initializing

                      >On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comca st.netwrote:
                      [edited to mininal example]
                      >>struct foo {
                      > int a1;
                      > int a2;
                      >>} foobar[10] = {
                      > [0].a1 = 10,
                      > [2] = {20, 2}
                      >>};
                      In article <1173542926.948 439.307490@v33g 2000cwv.googleg roups.com>
                      Rajesh S R <SRRajesh1989@g mail.comwrote:
                      >I am not able to understand how the above initialisation is related
                      >with Compound literals ...
                      It is unrelated to "compound literals". The above is an example of
                      "designated initializers".

                      A "compound literal" is a syntactic entity, consisting of two
                      main parts. The first part looks exactly like a cast: it has
                      a type-name enclosed in parentheses. The second part looks
                      exactly like an aggregate initializer: it has values enclosed
                      in braces. So:

                      (int) { 2 }

                      or:

                      (struct foo) { 9, 7 }

                      are both examples of compound literals.

                      A "designated initializer" is also a synactic entity, consisting
                      of two main parts. The second part is any ordinary initializer
                      (which may or may not be enclosed in braces as usual); the first
                      part is the designator, consisting of things like "[2] =" or
                      ".a1 =".

                      You can combine designated initializers with compound literals,
                      as in:

                      (struct foo) { .a2 = 42 }

                      Here the compound literal contains a designated initializer, so
                      this makes a "struct foo" object -- whose storage duration depends
                      on where the compound literal appears in the source -- whose value
                      is { 0, 42 }, i.e., the a1 member is initialized to 0, and the a2
                      member is initialized to 42.

                      Both designated initializers and compound literals are C99-specific,
                      hence not supported by many C (C89 or C95) compilers. (C89 and
                      C90 are the same language: C89 here refers to the 1989 ANSI C
                      standard, X3.159-9899, while C90 refers to the 1990 ISO C standard,
                      ISO 9899-1990. While the two standards differ, it is mainly in
                      section numbering; both describe the same language.)
                      --
                      In-Real-Life: Chris Torek, Wind River Systems
                      Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                      email: forget about it http://web.torek.net/torek/index.html
                      Reading email is like searching for food in the garbage, thanks to spammers.

                      Comment

                      • Rajesh S R

                        #12
                        Re: skipping a structure member while initializing

                        On Mar 10, 10:49 pm, Chris Torek <nos...@torek.n etwrote:
                        On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comca st.netwrote:
                        >
                        [edited to mininal example]
                        >
                        >struct foo {
                        int a1;
                        int a2;
                        >} foobar[10] = {
                        [0].a1 = 10,
                        [2] = {20, 2}
                        >};
                        >
                        In article <1173542926.948 439.307...@v33g 2000cwv.googleg roups.com>
                        Rajesh S R <SRRajesh1...@g mail.comwrote:
                        >
                        I am not able to understand how the above initialisation is related
                        with Compound literals ...
                        >
                        It is unrelated to "compound literals". The above is an example of
                        "designated initializers".
                        >
                        A "compound literal" is a syntactic entity, consisting of two
                        main parts. The first part looks exactly like a cast: it has
                        a type-name enclosed in parentheses. The second part looks
                        exactly like an aggregate initializer: it has values enclosed
                        in braces. So:
                        >
                        (int) { 2 }
                        >
                        or:
                        >
                        (struct foo) { 9, 7 }
                        >
                        are both examples of compound literals.
                        >
                        A "designated initializer" is also a synactic entity, consisting
                        of two main parts. The second part is any ordinary initializer
                        (which may or may not be enclosed in braces as usual); the first
                        part is the designator, consisting of things like "[2] =" or
                        ".a1 =".
                        >
                        You can combine designated initializers with compound literals,
                        as in:
                        >
                        (struct foo) { .a2 = 42 }
                        >
                        Here the compound literal contains a designated initializer, so
                        this makes a "struct foo" object -- whose storage duration depends
                        on where the compound literal appears in the source -- whose value
                        is { 0, 42 }, i.e., the a1 member is initialized to 0, and the a2
                        member is initialized to 42.
                        >
                        Both designated initializers and compound literals are C99-specific,
                        hence not supported by many C (C89 or C95) compilers. (C89 and
                        C90 are the same language: C89 here refers to the 1989 ANSI C
                        standard, X3.159-9899, while C90 refers to the 1990 ISO C standard,
                        ISO 9899-1990. While the two standards differ, it is mainly in
                        section numbering; both describe the same language.)
                        --
                        In-Real-Life: Chris Torek, Wind River Systems
                        Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                        email: forget about it http://web.torek.net/torek/index.html
                        Reading email is like searching for food in the garbage, thanks to spammers.

                        Thank you very much for your explanations.
                        I understand it now.

                        Comment

                        • David Thompson

                          #13
                          Re: skipping a structure member while initializing

                          On 10 Mar 2007 17:49:49 GMT, Chris Torek <nospam@torek.n etwrote:

                          <snip good but uncharacteristi cally brief answer <G>>
                          Both designated initializers and compound literals are C99-specific,
                          hence not supported by many C (C89 or C95) compilers. (C89 and
                          C90 are the same language: C89 here refers to the 1989 ANSI C
                          standard, X3.159-9899, while C90 refers to the 1990 ISO C standard,
                          I doubt X3 (now X3'') or even ANSI will remain in existence that long.
                          ISO 9899-1990. While the two standards differ, it is mainly in
                          section numbering; both describe the same language.)
                          You wanted X3.159- *1989* of course.

                          And to be picky, it's ISO/IEC 9899:1990, because ISO uses hyphen for
                          parts and colon for year, like 1539-1:year and 1539-2:year for Fortran
                          and 13818-MANY:year for MPEG.

                          Comment

                          Working...