GCC structure initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Svein E. Seldal

    #1

    GCC structure initialization

    Hello,

    I have this:

    struct mystruct {
    int a;
    union {
    int b;
    int c;
    };
    };

    When I initialize it:

    struct mystruct implement = {
    .a = 1,
    .b = 2
    };

    I get the following error from gcc:

    error: unknown field `b' specified in initializer

    Is it possible to do this kind of initialization alltogether with gcc?
    How can I initialize the anonymous union in the struct listed above?

    I know this is a gcc feature, but I wondered if there anyone here that
    might know this, please.


    Thanks,
    Svein
  • Alex

    #2
    Re: GCC structure initialization

    "Svein E. Seldal" <Svein_dot_Seld al@solidas.com> wrote in message
    news:7Y1pc.1453 $RL3.31498@news 2.e.nsc.no...
    [snip][color=blue]
    > How can I initialize the anonymous union in the struct listed above?[/color]

    By making it not anonymous? Questions regarding GCC-specific features should
    be asked in a GCC group. Although IIRC, C99 has something similar (or
    identical).

    Alex


    Comment

    • Martin Ambuhl

      #3
      Re: GCC structure initialization

      Svein E. Seldal wrote:
      [color=blue]
      > Hello,
      >
      > I have this:
      >
      > struct mystruct {
      > int a;
      > union {
      > int b;
      > int c;
      > };
      > };
      >
      > When I initialize it:
      >
      > struct mystruct implement = {
      > .a = 1,
      > .b = 2
      > };
      >
      > I get the following error from gcc:
      >
      > error: unknown field `b' specified in initializer
      >
      > Is it possible to do this kind of initialization alltogether with gcc?
      > How can I initialize the anonymous union in the struct listed above?
      >
      > I know this is a gcc feature, but I wondered if there anyone here that
      > might know this, please.[/color]

      Anonymous unions are off-topic here. A gnu* newsgroup would do better.
      Obviously, naming the union would work here.

      <ot>
      In your case either
      struct mystruct implement = {
      .a = 1, {2}
      };
      or
      struct mystruct implement = {
      .a = 1
      };
      implement.b = 2;
      should work.
      </ot>

      Comment

      Working...