Union initialization

Charles Bryant ch at maths.tcd.ie
Sat Feb 25 07:09:31 AEST 1989


In article <51116 at yale-celray.yale.UUCP> wald-david at CS.YALE.EDU (david wald) writes:
>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?
>>
.
.
.
>Any suggestions?

Given:
	If a union has two members with the same type, the compiler need
	not distinguish between them.

How about:
	union {
		float f;
		double d;
		int i;
		char c;
	} foo = { 1 };		/* initialises i */

OR	} foo = { (float) 1.1 };	/* f */
OR	} foo = {(double) 1.1 };	/* d */
OR	} foo = { (char) 'a'};		/* c */

Perhaps this would be too much of a special case for the compiler (it
otherwise dosen't need to know that an expression is of type 'char' for
instance).

-- 

		Charles Bryant.
Working at Datacode Electronics Ltd.



More information about the Comp.lang.c mailing list