This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Anonymous Unions


GCC definitely supports anonymous unions, however the way you have declared
it means that it will be impossible to reference its members.  The only time
anonymous structs or unions are useful is when you are doing a typedef or
variable definition in the same declaration, eg

union {
    int a;
    char *b;
} x, y, *z;

or

typedef union {
    int a;
    char *b;
} MyUnion;

In this case, referencing the variable's name (or MyUnion, in the second
example) gives you the ability to access its members.

GCC will still compile the code, and the code it generates will do what the
source code says it should do.  The real issue is that GCC is brazen enough
to generate a warning, whereas the politically-correct compilers just carry
on compiling it without alerting you that there might be a problem with your
code ;-)

Stan Katz wrote:

> Does gcc support the concept of anonymous unions. I am running V2.8.1
> cross compiling from DOS (actually Windows) to Hitachi SH. Code that
> compiles under other tools (BSO, Borland, VC). I define a structure
> containing an unnamed union with 2 elements. The code refers to these
> elements as if they were members of the structure. e.g.
>
> struct Block {
>         int             number;
>         union {
>                 int     Value;
>                 char    *String;
>                 }
>         struct block    *next;
>         };
>
> ...
>
> block.Value = 1;
>
>  Under gcc this generates a warning : "unnamed struct/union that defines
> no instances" and an error "structure  has no member named 'Value' ".
>
> Anyone out there know of a compiler switch or something that will help,
> or do I have to add a name for the union?
>
> Stan Katz
>
> _______________________________________________
> New CrossGCC FAQ: http://www.objsw.com/CrossGCC
> _______________________________________________
> To remove yourself from the crossgcc list, send
> mail to crossgcc-request@cygnus.com with the
> text 'unsubscribe' (without the quotes) in the
> body of the message.

_______________________________________________
New CrossGCC FAQ: http://www.objsw.com/CrossGCC
_______________________________________________
To remove yourself from the crossgcc list, send
mail to crossgcc-request@cygnus.com with the
text 'unsubscribe' (without the quotes) in the
body of the message.