Need help with union trick: summary of replies

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Sat Jan 5 09:17:01 AEST 1991


In article <1991Jan4.192645.7094 at lavaca.uh.edu> jet at karazm.math.uh.edu ("J. Eric Townsend") writes:
> I received many replies, some using #defines, others requiring other
> changes.  I think the following message best summaraizes the answers that
> work the best.

The #define answers are the only safe ones so far. I wouldn't recommend
any of the others for anything you're planning to use on several
machines.

  [ ANSI anonymous structures: ]
> |  typedef union pointstruct {
> |      struct {
> |	  double x, y, z;
> |      };
> |      double foo [3];
> |  } Point;

This is not guaranteed to work because the structure elements might not
be contiguous, or even in order.

The right solution for a language designer is context definitions:

  typedef struct pointstruct
   {
    double foo[3];
    define x foo[0];
    define y foo[1];
    define z foo[2];
   }
  Point;

This would solve the same problems as ANSI anonymous structures but
would be a lot safer for unions.

---Dan



More information about the Comp.lang.c mailing list