[comp.lang.c] Downward compatible extension to unions

hascall@cs.iastate.edu (John Hascall) (02/14/90)

The more I think about the ANSI standard for union initialization, the
more I fail to understand it.  Was the following suggested, and if so,
what were the reasons for rejecting it?

   Make union initialization parallel to struct initialization except that
   only one element may be specified.

      struct FOO {                      union FOO {
          char  bar;                        char  bar;
          int   baz;                        int   baz;
          long  buz;                        long  buz;
      } examp = { 'C', 42, 61762 };     } examp = { , 42, };

   Note that this method would not break existing code using the
   "initialize the first element" rule (assumming trailing commas
   were optional).

   The real usefulness is when you have an array of unions which you
   may want to be initialize differently:

      union FOO  things[3] = { {,42,}, {'C',,}, {,,61762}, };

John Hascall  /  ISU Comp Ctr, Ames IA  /  hascall@atanasoff.cs.iastate.edu

karl@haddock.ima.isc.com (Karl Heuer) (02/16/90)

In article <592@dino.cs.iastate.edu> hascall@cs.iastate.edu (John Hascall) writes:
>The more I think about the ANSI standard for union initialization, the
>more I fail to understand it.  Was the following suggested...

The problem is that you're thinking in terms of a feature that allows you to
initialize unions.  All that X3J11 really wanted was a way to unambiguously
specify what is the initial value of a static-storage-duration union that is
*not* explicitly initialized.  It happens that their decision also provides a
half-assed way to explicitly initialize *some* unions, but that's pretty much
an unintended side effect.

Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint