Union initialization

Chris Torek chris at mimsy.UUCP
Sun Feb 19 02:20:38 AEST 1989


In article <816 at atanasoff.cs.iastate.edu> hascall at atanasoff.cs.iastate.edu
(John Hascall) writes:
>Does 'ANSI' C allow for union initialization?

Because all static and global variables are initialised to 0 (cast to
the appropriate type), the pANS *must* allow for union initialisation,
else how could one talk about the initial value of a static or global
union?  But since members of a union may overlay one another, and
a 0 of one type may not match a zero of another, there must be some
rule for deciding *which* union member(s) are to be zero.

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.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list