Search code examples
cstructinitializationanonymousunions

How to initialise the second member of an anonymous structure in C?


I have made a structure for ints and pointers etc. as might be used in LISP.

A pointer is at least 8-byte aligned so tag=0. An integer is 29 bits and has a tag of 1. Other types have different tag values.

struct Atom{
  union{
    Pair              *pair;
    struct{
      unsigned        tag     :3;
      union{
        int           val     :29;
        char          ch;
        struct{
          int         mant    :21;
          Exp         exp     :8;
        };
      };
    };
  };
};

I would like to initialise them differently.

For pointers:

Atom aPair = {{.pair=0}}; // works

or

Atom aPair = {{0}}; //works

This works because, I assume, GCC assumes that I want to initialise the first member of the union.

I'd also like to initialise an integer - something like this:

Atom anInt={{ {.tag=1,{.val=0} } }};

I know this is not standard C, but is this possible at all with anonymous structures in GCC?


Solution

  • It is a known bug.

    ... which has been fixed in gcc 4.6 (using struct Atom anInt={{ .tag=1, {.val=0} }};).