initialising anonymous unions/structures

Johan Rydberg jrydberg@gnu.org
Wed Feb 16 08:19:00 GMT 2005


Hi,

I'm having a bit of trouble initializing a union which contains a
anonymous structure.  Here's what it looks like:

  union foo
  {
    int raw;
    struct {
      int x : 16;
      int y : 16;
    };
  };

It is possible to reference "x" and "y" in the same scope as "raw".
Example:

  union foo foo = { .raw = XY };
  printf ("%x\n", foo.x);

But I can't for the love of my life initialize "x" and "y" in a static
construct;

  union foo bar =
  {
    .x = 10,
    .y = 20
  };

Just gives me the following errors:

  bar.c:13: unknown field `x' specified in initializer
  bar.c:14: unknown field `y' specified in initializer

Are there some other way to refer to "x" and "y"?  I have tried things
like: 

  union foo bar =
  {
    0,
    {
      .x = 10,
      .y = 20
    }
  };

Without luck:

  bar.c:14: extra brace group at end of initializer
  bar.c:14: (near initialization for `bar')

Looking through the ML archives I found [1], so it seems that I'm not
the only one having this problem.

 [1] http://gcc.gnu.org/ml/gcc/2003-10/msg00708.html




More information about the Gcc-help mailing list