Need help with union trick

J. Eric Townsend jet at karazm.math.uh.edu
Thu Jan 3 11:57:00 AEST 1991



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 at uh.edu    Bitnet: jet at UHOU
Systems Mangler - UH Dept. of Mathematics - (713) 749-2120
Skate UNIX, boyo.



More information about the Comp.lang.c mailing list