unions or automatic aggregates cannot be initialized. Why?

Henry Spencer henry at utzoo.uucp
Mon Jan 1 11:30:13 AEST 1990


In article <915 at mbf.UUCP> kris at mbf.UUCP (A. Krishna Murthy) writes:
>	In the definitive text 'The C Programming Language' by
>	Brian W kernighan and Dennis M Ritchie...
>	"It is not permitted to inititalize unions or automatic aggregates."
>	Why?

The new definitive text is the 2nd edition of K&R, which documents recent
changes to this rule.

Speaking strictly of the old rule, however...  The problem with initializing
unions is deciding which member is to be initialized.  You can't do it by
looking at the type of the initializer; it is easy to construct cases where
this is ambiguous.  One has to invent some new notation to pick the member.
Dennis never quite got around to it.  (The new rule is that initializers
apply to the first member, always.  This is more a matter of giving unions
a well-defined initial value than of providing a useful way to initialize
them, obviously.)

The problem with automatic aggregate initialization was that it's an
implementation nuisance.  Initialization of static aggregates just
requires setting up the memory to be in that state initially, while
initializing automatic aggregates requires generating code to do the
work at run time.  (Automatic aggregate initialization is now permitted,
subject to some small restrictions.)
-- 
1972: Saturn V #15 flight-ready|     Henry Spencer at U of Toronto Zoology
1989: birds nesting in engines | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list