Initializing unions

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

    Initializing unions

    Given a union definition:

    union problem_t {
    int mask[2];
    struct {
    int indices[2];
    int ops[2];
    } comp;
    };

    Is it possible to initialize the struct containing the two arrays when
    declaring a variable of this type? I've tried just about every
    combination I can think of, but I can't seem to get it to work. For
    example:

    int main() {
    problem_t problem = { {}, { { 0, 0 }, { 0, 0 } } };
    }

    union.cc: In function `int main()':
    union.cc:12: excess elements in aggregate initializer

    I've tried omitting the empty array representing the first element of the
    union and that doesn't work either. I can't simply reverse the two union
    elements, because I initialize it differently in different parts of my
    program depending on how I want the union used.

    Any ideas? Thanks in advance...

  • Andrey Tarasevich

    #2
    Re: Initializing unions

    Josh Lessard wrote:[color=blue]
    > Given a union definition:
    >
    > union problem_t {
    > int mask[2];
    > struct {
    > int indices[2];
    > int ops[2];
    > } comp;
    > };
    >
    > Is it possible to initialize the struct containing the two arrays when
    > declaring a variable of this type?[/color]

    No. You'll have to use assignment instead of initialization

    (Unless you have another union, which already holds the desired values.
    In that case you can copy initialize the new union from the old one.)

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Sam Dennis

      #3
      Re: Initializing unions

      Josh Lessard wrote:[color=blue]
      > union problem_t {
      > int mask[2];
      > struct {
      > int indices[2];
      > int ops[2];
      > } comp;
      > };
      >
      > Is it possible to initialize the struct containing the two arrays when
      > declaring a variable of this type?[/color]

      With a designator, yes:

      union problem_t problem = { .comp = { { 0, 0 }, { 0, 0 } } };

      If you want your code to be portable to pre-C99 implementations , though,
      you'll just have to use an assignment to achieve this.

      --
      ++acr@,ka"

      Comment

      • Jack Klein

        #4
        Re: Initializing unions

        On Sat, 6 Mar 2004 02:14:54 +0000 (UTC), Sam Dennis
        <sam@malfunctio n.screaming.net > wrote in comp.lang.c++:
        [color=blue]
        > Josh Lessard wrote:[color=green]
        > > union problem_t {
        > > int mask[2];
        > > struct {
        > > int indices[2];
        > > int ops[2];
        > > } comp;
        > > };
        > >
        > > Is it possible to initialize the struct containing the two arrays when
        > > declaring a variable of this type?[/color]
        >
        > With a designator, yes:
        >
        > union problem_t problem = { .comp = { { 0, 0 }, { 0, 0 } } };
        >
        > If you want your code to be portable to pre-C99 implementations , though,
        > you'll just have to use an assignment to achieve this.[/color]

        There is a version of C, not particularly widely available
        unfortunately, that will accept this definition with initialization.

        But there is no version of C++ that does, so why post it here?

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        • Sam Dennis

          #5
          Re: Initializing unions

          Jack Klein wrote:[color=blue]
          > On Sat, 6 Mar 2004 02:14:54 +0000 (UTC), Sam Dennis
          ><sam@malfuncti on.screaming.ne t> wrote in comp.lang.c++:[color=green]
          >> [C99 feature][/color]
          >
          > But there is no version of C++ that does, so why post it here?[/color]

          My mistake; I read both groups and got confused.

          --
          ++acr@,ka"

          Comment

          • Jack Klein

            #6
            Re: Initializing unions

            On Sat, 6 Mar 2004 05:47:59 +0000 (UTC), Sam Dennis
            <sam@malfunctio n.screaming.net > wrote in comp.lang.c++:
            [color=blue]
            > Jack Klein wrote:[color=green]
            > > On Sat, 6 Mar 2004 02:14:54 +0000 (UTC), Sam Dennis
            > ><sam@malfuncti on.screaming.ne t> wrote in comp.lang.c++:[color=darkred]
            > >> [C99 feature][/color]
            > >
            > > But there is no version of C++ that does, so why post it here?[/color]
            >
            > My mistake; I read both groups and got confused.[/color]

            No problem, I make my own share of mistakes, I just didn't want the OP
            complaining that his C++ compiler didn't support this and it should...

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++

            Comment

            • Nick Hounsome

              #7
              Re: Initializing unions


              "Josh Lessard" <jrlessar@plg2. math.uwaterloo. ca> wrote in message
              news:Pine.SOL.4 .44.04030520343 40.425-100000@plg2.mat h.uwaterloo.ca. ..[color=blue]
              > Given a union definition:
              >
              > union problem_t {
              > int mask[2];
              > struct {
              > int indices[2];
              > int ops[2];
              > } comp;
              > };
              >
              > Is it possible to initialize the struct containing the two arrays when
              > declaring a variable of this type? I've tried just about every
              > combination I can think of, but I can't seem to get it to work. For
              > example:
              >
              > int main() {
              > problem_t problem = { {}, { { 0, 0 }, { 0, 0 } } };
              > }
              >
              > union.cc: In function `int main()':
              > union.cc:12: excess elements in aggregate initializer
              >
              > I've tried omitting the empty array representing the first element of the
              > union and that doesn't work either. I can't simply reverse the two union
              > elements, because I initialize it differently in different parts of my
              > program depending on how I want the union used.
              >
              > Any ideas? Thanks in advance...
              >[/color]

              ref 8.5.1
              .....the braces shall only contain an initializer for the first member of the
              union.

              so reorder your union and treat it like it was just the first member.

              Alternatively add one or more constructors.


              Comment

              • Josh Lessard

                #8
                Re: Initializing unions

                On Sat, 6 Mar 2004, Nick Hounsome wrote:
                [color=blue]
                >
                > "Josh Lessard" <jrlessar@plg2. math.uwaterloo. ca> wrote in message
                > news:Pine.SOL.4 .44.04030520343 40.425-100000@plg2.mat h.uwaterloo.ca. ..[color=green]
                > > Given a union definition:
                > >
                > > union problem_t {
                > > int mask[2];
                > > struct {
                > > int indices[2];
                > > int ops[2];
                > > } comp;
                > > };
                > >
                > > Is it possible to initialize the struct containing the two arrays when
                > > declaring a variable of this type? I've tried just about every
                > > combination I can think of, but I can't seem to get it to work. For
                > > example:
                > >
                > > int main() {
                > > problem_t problem = { {}, { { 0, 0 }, { 0, 0 } } };
                > > }
                > >
                > > union.cc: In function `int main()':
                > > union.cc:12: excess elements in aggregate initializer
                > >
                > > I've tried omitting the empty array representing the first element of the
                > > union and that doesn't work either. I can't simply reverse the two union
                > > elements, because I initialize it differently in different parts of my
                > > program depending on how I want the union used.
                > >
                > > Any ideas? Thanks in advance...
                > >[/color]
                >
                > ref 8.5.1
                > ....the braces shall only contain an initializer for the first member of the
                > union.
                >
                > so reorder your union and treat it like it was just the first member.
                >
                > Alternatively add one or more constructors.[/color]

                Thank you very much for your suggestions, but I've already considered
                them. Reordering the union members won't help because the size of the
                'mask' array is different for each platform I'm implementing my program on
                (the size is actually set by a platform specific const int), and there are
                times where I need to fill that entire array.

                Constructors also won't work because I'm trying to initialize an array of
                these unions. I guess I'll just have to make it a struct, and thus use an
                extra four bytes for the two arrays of two elements.

                Thanks everyone for your suggestions.

                Comment

                • Josh Lessard

                  #9
                  Re: Initializing unions

                  On Sat, 6 Mar 2004, Josh Lessard wrote:
                  [color=blue]
                  > Constructors also won't work because I'm trying to initialize an array of
                  > these unions. I guess I'll just have to make it a struct, and thus use an
                  > extra four bytes for the two arrays of two elements.[/color]

                  Erm...extra 4 *ints* for the two arrays of two elements. Sorry about
                  that.

                  Comment

                  Working...