Structure question (problem)

Tom Duff td at alice.UUCP
Fri May 31 02:09:26 AEST 1985


It is very difficult to formulate a reasonable rule for union
initialization.  The ANSI rule (initializers apply to the first member)
is just a kludge.  The `match the types' rule plays fast and loose
with C's type-matching rules.  Consider ucbcat!faustus's example
(here simplified):
union{
	int a;
	long b;
}c[]={
	57,
	(long)76365
};
ucbcat!faustus no doubt expects 57 to initialize a and 76365 to
initialize b.  But 57 is a legal initializer for b and (long)76365
is a legal initializer for a.
So the `match the types' rule is not a simple patch to the language,
but requires a completely different set of type-matching rules than
already exist.  This is, to say the least, unsatisfactory.

Furthermore, this scheme cannot even be made to work.  The following
example should indicate one reason:

union{
	int a;
	struct{
		int :16;
		int b;
	}c;
}d={5};



More information about the Comp.lang.c mailing list