[net.lang.c] union initialization

grahamr (12/18/82)

Consider the declaration

	union utag {
		char *p;
		char c[4];
	};

Currently, initialization of unions is not allowed by the
language.  How about allowing initialization with this:

	union utag u.c = "foo";

It would declare u and initialize the bits in it that belong
to member c as shown.  Leftover bits are set to 0.  This syntax
naturally disallows initialization of more than one member.

zrm (12/21/82)

Yes! Legalize union initialization! But please lets have some reasonable
convesion for what gets what value. Whitesmith's C, as I remember
*requires* that structures be initialized and the rules are that the
value you initialize with must be of the type of the first object in the
union. The reason for such a rule becomes clear in an  example just like
the one submitted by the person sugesting this extension.

union foo
{
	char *bar;
	char baz[4];
}
mumble = "quux";

In the absence of hard fast rules, this could have several meanings. 1)
"The right thing" -- Initialize baz to contain "quux". 2) Or it could
mean initialize bar to *point* to the constant string "quux". Clearly
wrong, but since when do compilers know from wrong? 3) Or, perhaps even
more horribly, it could stuff q, u, u, and x in the location of the
symbol bar, but in the wrong byte order!

Yow! Are we suffering in a Safeway?

Cheers,
Zig

alan@allegra.UUCP (11/17/83)

Ok, how do I do it?  Can it be done?


	struct
	{
		int x;
		int y;

		union
		{
			int z;
			char *p;
		} u;
	} foo = { 100, 200, 300 };


This doesn't work!


	Alan S. Driscoll
	AT&T Bell Laboratories

henry@utzoo.UUCP (Henry Spencer) (11/04/84)

> Was one of the alternatives [for union initialization] something similar
> to the ALGOLW "record constant" facility? In C, this would translate to:
> 
> 	(struct gezornin) {"a", 10, 2.3, 'x'}
> 
> being considered a constant. ...
> 
> In other words, add a facility to allow structure constants (useful in *lots*
> of places), and then require all union initializations to have an explicit
> cast.

I would imagine this was looked at.  One obvious problem with structure
constants is that there is no C implementation experience with them, so
we cannot be confident that we understand their implications and problems.
Remember, ANSI committees are not in the business of inventing new ideas,
but of cleaning up and standardizing well-proven ideas.  This is a feature,
not a bug; we do NOT want the ANSI C committee to start inventing a new
language.  Some would argue that the current drafts are already borderline.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry