[comp.lang.c] unions or automatic aggregates cannot be initialized. Why?

kris@mbf.UUCP (A. Krishna Murthy) (12/30/89)

	Hi folks,

	In the definitive text 'The C Programming Language' by
	Brian W kernighan and Dennis M Ritchie,
	Appendix A: C Reference Manual, section 8.6 Initialization,
	paragraph four, emphatically states

	"It is not permitted to inititalize unions or automatic aggregates."

	Why?

	Note:
	The above statement is with regard to the definition of the language
	and NOT to any implementation difficulties.

	Answers will be very much appreciated.

	kris.

henry@utzoo.uucp (Henry Spencer) (01/01/90)

In article <915@mbf.UUCP> kris@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@zoo.toronto.edu

meager%playland@Sun.COM (Michael J. Eager) (01/09/90)

In article <915@mbf.UUCP> kris@mbf.UUCP (A. Krishna Murthy) writes:
>
>	Hi folks,
>
>	In the definitive text 'The C Programming Language' by
>	Brian W kernighan and Dennis M Ritchie,
>	Appendix A: C Reference Manual, section 8.6 Initialization,
>	paragraph four, emphatically states
>
>	"It is not permitted to inititalize unions or automatic aggregates."
>
>	Why?
>
>	Note:
>	The above statement is with regard to the definition of the language
>	and NOT to any implementation difficulties.
>
>	Answers will be very much appreciated.
>
>	kris.


The ANSI Standard for C permits initialization of automatic
aggregates.  Not every compiler supports this, even when they claim
to support the ANSI Standard. 

(I'm not sure that I would describe K&R as definitive -- the ANSI Standard 
for C defines many behaviors which K&R merely imply.)

-- Michael J. Eager