[comp.lang.c] Need help with union trick

jet@karazm.math.uh.edu ("J. Eric Townsend") (01/03/91)

I don't think this is possible in C (K&R or ANSI), but maybe someone
can enlighten me as to the hows and whys of why it is/isnt' possible.

I have a body of code with the following structure:
typedef struct pointstruct {
  double x,y,z;
  } Point;

I would like to be able to reference the elements as an array without
having to rewrite the existing code. (We've got code to be pasted in that
relies on being able to grab the x, y and z as array indicies. Grr. :-)
ie:
foo.x = 1.0; /* or */ foo[0] = 1.0;

Here's the bind, as I see it.:

Point babble[100];
a = babble[0].x; /* vs  */ a = babble[0][0];

This:
typedef union pointstruct {
  double x,y,z;
  double foo[3];
  } Point;
would union x, y and z to foo[0], which is not what I want...

Again, I'm not sure if there's any way to do this.

(The current solution is to have the "magic copy" routine, which is ugly
and slow, but gets the job done.)
--
J. Eric Townsend     Internet: jet@uh.edu    Bitnet: jet@UHOU
Systems Mangler - UH Dept. of Mathematics - (713) 749-2120
Skate UNIX, boyo.

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (01/03/91)

In article <1991Jan3.005700.20623@lavaca.uh.edu> jet@karazm.math.uh.edu ("J. Eric Townsend") writes:
> I don't think this is possible in C (K&R or ANSI), but maybe someone
> can enlighten me as to the hows and whys of why it is/isnt' possible.
> I have a body of code with the following structure:
> typedef struct pointstruct {
>   double x,y,z;
>   } Point;
> I would like to be able to reference the elements as an array without
> having to rewrite the existing code. (We've got code to be pasted in that
> relies on being able to grab the x, y and z as array indicies. Grr. :-)

Well, the structure doesn't guarantee array alignment, so your basic
declaration has to be an array. Hence:

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

This puts x, y, and z into the main namespace, and it may not be fully
efficient on some machines. If I were desperate I might try

  typedef struct pointstruct
   {
    double x; double y; double z;
   }
  Point;

  typedef union pointunion
   {
    struct pointstruct p;
    double a[3];
   }
  PU;

I seem to remember from previous discussions that (PU *) &p may not be
aligned correctly, where p has type Point. But if ((PU *) &p).a[0] has
the same address as p.x, ...a[1] as p.y, and ...a[2] as p.z, then you're
going to be safe in practice. If you think your CPU is going to blow up
when you try to compare &(((PU *) &p).a[0]) with &(p.x), don't use this
method.

---Dan

ping@cubmol.bio.columbia.edu (Shiping Zhang) (01/03/91)

In article <1991Jan3.005700.20623@lavaca.uh.edu> jet@karazm.math.uh.edu ("J. Eric Townsend") writes:
 
 
>I have a body of code with the following structure:
>typedef struct pointstruct {
>  double x,y,z;
>  } Point;
 
>I would like to be able to reference the elements as an array without
>having to rewrite the existing code. (We've got code to be pasted in that
>relies on being able to grab the x, y and z as array indicies. Grr. :-)



How about this

typedef double Point[3];

-ping