[net.lang.c] a better syntax for unions

andrew (03/25/83)

I also dislike the extra identifier required in references to members
of unions within structures, and I applaud Lee Iverson's approach to a
remedy, which closely resembles a useful feature of PL/I.  However, I
must point out some aspects in which this clashes with historical C.

The suggestion is that, for example, if the structure in question is:

	struct {
		int a;
		union {
			long l_b;
			float f_b;
			} u;
		} s;

that one be able to say "s.l_b" instead of "s.u.l_b".  Lee's solution
is to hack PCC so that member tags are searched for throughout the
structure, rather than only at the top level.

If you look back to earlier versions of C, which were more lax with
regard to matching tags with their structures, the construct "s.l_b"
above would mean to interpret the "s" structure as though it were in
fact a "u" union.  Thus, "s.l_b" on, say, a PDP-11 is the longword
occupying the first four bytes of the structure, rather than the
longword in the third through sixth bytes, as is intended.

Compilers that allow this also let you specify:

	struct foo {int _l;}
	0177400 -> _l = 0;

to clear the device register at location 0177400, rather than

	(struct foo *) 0177400 -> _l = 0;

I for one am willing to toss the historical behavior in favor of
the proposal.

  -- Andrew Klossner (decvax!tektronix!tekecs!andrew)

ka (03/29/83)

I have never liked the extra level introduced by the union concept.
Basicly, the union declaration does two things:  it overlays the
specified variables, and it places the variables in a structure.
Why not an overlay declaration which overlays variables without
putting them in a structure?  If the programmer wants the elements
of the overlay to be in a structure, he/she can declare the struct-
ure explicitly.  For example,

	union u {
		int ivar;
		float fvar;
	} un;

could be written as

	struct u {
		overlay {
			int ivar;
			float fvar;
		}
	} un;

The union construct is simpler here because it avoids an extra level of
nesting, but the second construct may be clearer because two unrelated
operations, overlaying variables and placing variables into a structure,
are not combined into one construct.  More importantly, in most cases
overlayed variables don't belong in a structure.  The C union construct
forces you to put them in a structure anyway, obscuring your program.
				Kenneth Almqust