[comp.lang.c] initialization of automatic structures/unions

filisa@albert.ai.mit.edu (Filisa Vistima) (02/28/91)

If such a construct such as this is illegal (says my compiler):

	struct Stuff { int i, j, k, l; };

	int main(argc, char ** argv)  {
		struct Stuff stuffy = { 0, 0, 0, 0 };  /* illegal part */
	}

Can someone give me a good reason why initializing automatic
structures/unions is illegal?

Thanks.

filisa

henry@zoo.toronto.edu (Henry Spencer) (03/02/91)

In article <13599@life.ai.mit.edu> filisa@albert.ai.mit.edu (Filisa Vistima) writes:
>Can someone give me a good reason why initializing automatic
>structures/unions is illegal?

Because your compiler is old.
-- 
"But this *is* the simplified version   | Henry Spencer @ U of Toronto Zoology
for the general public."     -S. Harris |  henry@zoo.toronto.edu  utzoo!henry

torek@elf.ee.lbl.gov (Chris Torek) (03/02/91)

In article <13599@life.ai.mit.edu> filisa@albert.ai.mit.edu
(Filisa Vistima) writes:
>Can someone give me a good reason why initializing automatic
>structures/unions is illegal? (says my compiler)

It is illegal because your compiler is (choose one):

  (a) broken;
  (b) not ANSI C conformant.

X3.159-1989 allows initialization of automatic (`local') structure and
union variables, with the same syntax and semantics as for static
variables (with the obvious change that the values do not persist).
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov

stachour@sctc.com (Paul Stachour) (03/02/91)

filisa@albert.ai.mit.edu (Filisa Vistima) writes:

>If such a construct such as this is illegal (says my compiler):
>	struct Stuff { int i, j, k, l; };
>	int main(argc, char ** argv)  {
>		struct Stuff stuffy = { 0, 0, 0, 0 };  /* illegal part */
>	}
>Can someone give me a good reason why initializing automatic
>structures/unions is illegal?

    Because according to the rules of initialization:
Initializers follow the sames rules for type and conversion as do
assignment statements, and one can't assign "constant structures"
or "constant arrays" in C.

    If C followed the "rule of least asstonishment" (which it does
not), then the operations of:

     Assignment
     Initialization
     Comparison

would be "equivalent" in what one could compare, assign, and initialize.

     But C has NEVER understood this principle.  I recommend you use
a programming language like Ada, which was designed for software
engineering.  Then you will have many fewer of the "why is this
incosistant with that" kind of questions.

      ...Paul
-- 
Paul Stachour         Secure Computing Technology Corp
stachour@sctc.com      1210 W. County Rd E, Suite 100           
		 	   Arden Hills, MN  55112
                             [1]-(612) 482-7467

dsebbo@dahlia.uwaterloo.ca (David Ebbo) (03/02/91)

In article <13599@life.ai.mit.edu> filisa@albert.ai.mit.edu (Filisa Vistima) writes:
>If such a construct such as this is illegal (says my compiler):
>
>	struct Stuff { int i, j, k, l; };
>
>	int main(argc, char ** argv)  {
>		struct Stuff stuffy = { 0, 0, 0, 0 };  /* illegal part */
>	}
>
>Can someone give me a good reason why initializing automatic
>structures/unions is illegal?
>
>Thanks.

It's not illegal.  You're just using a compiler that's not very standard.
Also, although this is not related to your problem, you forgot to give a type
to argc (i.e. you should have 'int argc').

David Ebbo.

henry@zoo.toronto.edu (Henry Spencer) (03/03/91)

In article <1991Mar2.000247.11318@sctc.com> stachour@sctc.com (Paul Stachour) writes:
>>Can someone give me a good reason why initializing automatic
>>structures/unions is illegal?
>
>    Because according to the rules of initialization:
>Initializers follow the sames rules for type and conversion as do
>assignment statements, and one can't assign "constant structures"
>or "constant arrays" in C.

Oh really?  Initialization of static structs has been possible since the
dawn of time in C, which makes your statement complete nonsense.  Please
get your facts straight before you post.  You don't help the credibility
of your Ada-good-C-bad claims by such gross errors.

The issue at hand is why Mr. Vistima can do this for statics but not for
automatics; the answer is "because his compiler doesn't implement ANSI C".
-- 
"But this *is* the simplified version   | Henry Spencer @ U of Toronto Zoology
for the general public."     -S. Harris |  henry@zoo.toronto.edu  utzoo!henry