[net.lang.c] Structure initialization rules question

noemi@moscom.UUCP (Noemi Berry) (05/21/86)

	Why is it that you can only initialize structures when they are global
	(outside of main() )?  If you initialize a structure within a function
	you get an "No auto. aggregate initialization" error.  I don't see
	anywhere in K&R where the structure initialization rules are explained.

	e.g.:

*--------------------- Compiles
struct XX {
	int i;
} xx = { 0 };

main()
{
}
------------------------------*

*--------------------- Doesn't compile
main()
{
	struct XX {
		int i;
	} xx = { 0 };

}
-------------------------------------*

	Is there some good reason for this?  (Is it a language inconsistency?)
	Is there something obvious I'm missing here?!

							noemi
-----------
noemi berry
{allegra|decvax|seismo}!rochester!moscom!noemi
"On a souvent besoin de plus petit que soi"

dave@ur-helheim.UUCP (Raver Dave) (05/22/86)

In article <696@moscom.UUCP> noemi@moscom.UUCP (Noemi Berry) writes:
>
>	Why is it that you can only initialize structures when they are global
>	(outside of main() )? 

>noemi berry
>{allegra|decvax|seismo}!rochester!moscom!noemi
>"On a souvent besoin de plus petit que soi"

K&R p120 (as in the index):
"An 'external' or 'static' structure can be initialized by...
a list of initializers for the components."

So the 'reason' is most likely that this initialization occurs
at compile time and not at runtime, since the latter could be very
costly.

Raver Dave

meissner@dg_rtp.UUCP (Michael Meissner) (05/25/86)

In article <696@moscom.UUCP> noemi@moscom.UUCP writes:
>
>	Why is it that you can only initialize structures when they are global
>	(outside of main() )?  If you initialize a structure within a function
>	you get an "No auto. aggregate initialization" error.  I don't see
>	anywhere in K&R where the structure initialization rules are explained.

    Under K&R (probably in appendix A where the real rules are spelled out)
it mentions that only aggreates (read arrays and structures) with static
storage (read static and global) can be initialized.  Ansi X3J11 (as of the
last meeting) allows initializing auto and register aggregates, providing
all members are constant.  The last constraint avoids some nasty problems
with regard to order of evaluation, and makes compilers unduely complex.

    As to why you could not initialize an auto aggregate, I think it was
because the PDP-11 did not have a block copy instruction, or that DMR did
not consider structures full fledged items (remember no structure arguments,
structure assignment, etc.).

	Michael Meissner, Data General
	...{ decvax, ihnp4, ... }!mcnc!rti-sel!dg_rtp!meissner