Unions and structures

David Chase rbbb at rice.ARPA
Thu Feb 21 01:04:02 AEST 1985


Someone has probably been over this before, but just in case, will someone
PLEASE grab the ear of someone on the ANSI committee and ask them to make
structures and unions first-class sort of datatypes.  Examples of problems
I have had using the Berkeley C compiler are:

1) "structure reference must be addressable"
================================================================
union foo {
  float f;
  int   i;
  };
union foo bar();
barf() {
  return bar().i;  /* This line gives the error */
  }
================================================================

2) "no automatic aggregate initialization"
================================================================
struct foo {
  float f;
  int   i;
  };
struct foo bar();
barf() {
  struct foo bozo = bar(); /* This line gives the error */
  }
================================================================

I find these restrictions fairly stupid and arbitrary, given that the
workarounds are so utterly trivial.  For the union, assigning into a
temporary and then returning the union element of the temporary works; for
the structure, just doing the declaration and the assignment in separate
statements passes.  I'm also a little hacked that the compiler blows off
register declarations for unions even when all members are the same
size and fit in a register (This of course is an implementation nit, not a
language specification, but programmers will be less likely to use unions
if they "know" the union will not be stored in a register.).  I wrote a
piece of code using unions hoping that it would be more portable than if I
just went casting a hunk of storage every-which-way, but now I find my
code is LESS portable because of antique compilers that won't let me
assign unions.

C needs a standard, even a bad one, just so I can quit second-guessing
what sort of brain damage I'm likely to encounter on the next machine I
use.  I'd also like to learn one language, not 3 or 4 that look enough
alike to confuse me.

Venting my frustrations on people who might be able to prevent them in the
future,
	David Chase



More information about the Comp.lang.c mailing list