# Structures - simple problem

This topic is 5476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm going hrough the NeHe OpenGL tutorials (which are excellent), and I'm up to tutorial 10. I've just come across something which I've seen before, but never quite understood. It concerns structures in c/c++..... In the tutorial, structures are defined for a SECTOR, TRIANGLE and VERTEX, as such: typedef struct tagSECTOR { int numtriangles; TRIANGLE* triangle; } SECTOR; typedef struct tagTRIANGLE { VERTEX vertex[3]; } TRIANGLE; typedef struct tagVERTEX { float x, y, z; float u, v; } VERTEX; If we take VERTEX as an example, why do we need to use the word "tagVERTEX" at the start of the structure definition? Surely the name of the structure is just "VERTEX" on its own? Thanks

##### Share on other sites
It's a C thing. In C, if you defined a struct as so:
struct Vertex{  float x;  float y;  float z;};
And you needed to use a variable of that type, then the declaration would look like this:
struct Vertex v;//Or for a function:struct Vertex Normalize(struct Vertex* pv);
Typedef-ing a "struct tagVertex" as "Vertex" means that you no longer need to bother with using the "struct" keyword everywhere.

In C++, you don't need to use the struct (or class) keyword like that anyway, so you can define a struct either way.

##### Share on other sites
it's so cool when u start writing your post and then find that someone else has already answered :P

##### Share on other sites
As far as I know, this is done to retain compatibility with C. In C, if you were to define a variable of type STRUCTURE1 (that is, a type defined as "struct STRUCTURE1 {};") the definition would have to include the keyword struct. More concisely, the following would have to be done:

struct STRUCTURE1 {// Declaration here};int main() {// Define variable of type STRUCTURE1struct STRUCTURE1 a;return 0;}

By using the typedef, and a "dummy" name (eg. tagVERTEX) you could simply type:

STRUCTURE1 a;

to create a variable. In C++ this is not necessary, since you do not need to write "struct" in front of every definition of a variable.

edit: just a tad late...

##### Share on other sites
Thanks for your help guys, much appreciated

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46