Standard for union initialization?

Kevin Martin kpmartin at watmath.UUCP
Mon Jan 14 13:32:07 AEST 1985


>Why not fix the syntax of that scheme and use its semantics:
>
>union {
>    <type1> foo;
>    <type2> bar;
>    <type3> mumble;
>} baz = (type3) <initializer>;
>
>Suppose the same type is duplicated in the union, e.g. suppose type3 is
>the same as type1 and the compiler doesn't know whether to initialize
>foo or mumble?  Let the compiler choose whichever valid one it wishes;
>the result would be equivalent.
>This would not make the language any messier; it is better than many
>other examples of casts.  And it would be useful, and it would not break
>existing programs.
>-- Norman Diamond
This only works if you either extend the syntax to allow "casting" of
aggregate initializers, or if you *still* don't allow initialization
of a union of structures.

Either way, this is less clear than an initializer of the form
	element = value
e.g.
union {
	<type1> foo;
	<type2> bar;
	<type3> mumble;
}baz = mumble = <initializer>;
Since <initializer> can be an expression in C already, you might find that
your compiler's grammar already allows this, and it is only detected as
an error after further analysis.

Of course, if the "element =" is absent, the first element could be
initialized. Similarly for the implicit zeroing of un-initialized
static storage.
            Kevin Martin, UofW Software Development Group.



More information about the Comp.lang.c mailing list