Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 this post

Link to post
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 STRUCTURE1
struct STRUCTURE1 a;
return 0;

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


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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!