[net.lang.c] Unions and structures

rbbb@rice.ARPA (David Chase) (02/20/85)

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

gwyn@Brl-Vld.ARPA (VLD/VMB) (02/21/85)

Your first example is legal under UNIX System V.
Your second example is not, but it will be according to
the Nov. 1984 draft proposed ANS for C.

henry@utzoo.UUCP (Henry Spencer) (02/25/85)

> 1) "structure reference must be addressable"

This one sounds like an out-and-out compiler bug, probably arising
because struct-returning functions were a very late addition to C.

> 2) "no automatic aggregate initialization"

Automatic aggregate initialization is definitely in ANSI C.

> ...  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...

As you note later, this is a compiler problem.  It's not a trivial thing
to fix in some compilers, though, because there are deeply-ingrained
assumptions that unions are memory objects.  Yes, it would be nice, but
the odds that a large percentage of implementors will make the effort
are poor.  Some will, but not enough that you could count on it.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry