Union initialization

David Goodenough dg at lakart.UUCP
Wed Feb 22 01:41:23 AEST 1989


>From article <51116 at yale-celray.yale.UUCP>, by wald-david at CS.YALE.EDU (david wald):
>>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};

Try:

union
 {
    float f;
    int i;
    char *c;
 } u[3] =
 {
    { 1.0 ; ; },	/* could also be { 1.0 } - trailing ; are optional */
    { ; 76 ; },		/* ditto: { ; 76 } would also be OK */
    { ; ; "STUG" }
 };

Not ambiguous at all - it is left as an excercise to the reader to figure
out what is happening. If you don't like the overloaded ';' then I suggest
you start bitching about ',' - that is overloaded far worse. :-P

BTW ....= { 1.0 ; 76 ; }, .....

WOULD NOT BE ALLOWED - it would generate some sort of complaint.
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.lang.c mailing list