• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# data structures

## Recommended Posts

Ive seen in some places a point data structure that is defined as the union of a float v[3] and float x, y, z;. What I mean is, the X coordinate can be accessed as either pt.x or pt.v[0], y as either pt.y or pt.v[2], and so on. The advatage ofcourse is readability, while still having the ability to use glVertex3fv and the other vector functions. Of course, it seems to be a general rule that when youre searching for something online that youve seen previously, you never find it. So I have no clue what the syntax is for the definition. I tried this: typedef struct t_point { union { struct { float x, y, z; }; float v[3]; }; } POINT; but that didnt work (I wouldnt be posting this if it did).

##### Share on other sites
union t_point
{
float x,y,z;
float v[3];
} POINT;

You should get yourself a good C++ book.

##### Share on other sites
or even a bad one. I picked up a US\$20 reference book a couple of weeks ago. It was an absolute score. I dont have it with me here, but it''s yellow and about the size of a 500page novel and it covers pretty much anything you''d ever want regarding c/c++. Brilliant stuff.

##### Share on other sites
some logical stuff : (arrays)

float v[3] ==
float* v = new float[3];

since pointers are always the size of 4, why should we refer
to a variable with the same size ???

or did I misunderstood something ?

I''m always using

float x,y,z;

because when you use arrays you have double (or more) referencing in a typedef !!!

##### Share on other sites
struct Point { union {  struct { float x,y,z; };  float v[3]; };};

this works for me, dunno your problem

##### Share on other sites
I'm using a casting operator to solve the problem.
struct V3f {  GLfloat x, y, z;  inline operator GLfloat*() {    return &x;  }};

Makes it possible to write code like this:
V3f v;// Access the individual elementsv.x = v.y = v.z = 0.5;// Use it as a vectorglVertex3fv( v );

Don't know if there are any performance or compiler issues with this usage though.

[edited by - nadam on March 18, 2002 5:23:14 PM]