Union initialization

david wald wald-david at CS.YALE.EDU
Mon Feb 20 06:55:44 AEST 1989


In article <16019 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>In article <816 at atanasoff.cs.iastate.edu> hascall at atanasoff.cs.iastate.edu
>(John Hascall) writes:
>>Does 'ANSI' C allow for union initialization?
>
>The rule is (perhaps overly) simple: the first member of the union
>is initialised.  Given
>
>       union { float f; int i; } u;
>
>u.f is 0.0, and u.i is indeterminte.  You may write
>
>       union { float f; int i; } u = { 1.0 };
>
>to set u.f, but you cannot initialise u.i since it is not the first
>member.

I wonder...

Yes, this question deals with some hypothetical C' or C+=2 (not quite D,
since it's a language extension rather than a revision), but...

Would it make the syntax more ambiguous to have allowed

union { float f; int i; } u.i = {1};

?

The only difficulty comes in extending this to structures containing
unions:

struct { char *cp; union { float f; int i; } u; } s = { NULL, ????};

Any suggestions?


============================================================================
David Wald                                              wald-david at yale.UUCP
waldave at yalevm.bitnet                                 wald-david at cs.yale.edu
"A monk, a clone and a ferengi decide to go bowling together..."
============================================================================



More information about the Comp.lang.c mailing list