typedef struct

Started by
2 comments, last by notme 21 years, 8 months ago
what exactly does typedef struct do? i.e. typedef struct { qboolean valid; int serverframe; int servertime; int deltaframe; byte areabits[MAX_MAP_AREAS/8]; player_state_t playerstate; int num_entities; int parse_entities; } frame_t; (taken from the Quake 2 source)
Advertisement
In C, typedef''ing a struct allows you to use the new name instead of "struct structname varname".


  typedef struct Ints {int I1;int I2;} Integers;//now, you can useIntegers struct1, struct2;//instead ofstruct Ints struct1, struct2;  


Basically, the typdef allows you to eliminate the "struct" part of the variable declaration. IIRC, this is not necessary in C++ - the "struct" keyword is not needed in variable declarations.

Hope this helps.

Ken Murphy
If you don''t understand structs perhaps the Quake 2 source isn''t a good place to look right now. I recommend you pick up a good book on C or C++.

Try a google search for "Thinking in C++" and "C++ In Action" for a couple of decent free online books.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
In C++, this:

typedef struct
{
} some_name;

is the same as this:

struct some_name
{
};

but the later is BY FAR the prefered way to do it.

as the first replied said ... this is an old C convention, because in C ... if you do this:

struct some_name
{
};

then when you declare varaibles you have to qualify them by telling the compiler it''s a struct name ... like this:

int x;
struct some_name myStruct;

but in C++ ... you NEVER use the word struct or class in that way, so the typedef around a struct or class declaration is really not used.

also .. note, there is no magic going on here .. what you are seeing is just a struct declaration, nested in a normal typedef. It would be the same as doing this as well:

struct some_struct
{
};

typedef some_struct another_struct;

in C .. to declare these as variables you would do it like this:

struct some_struct a;
another_struct b;

but in C++, they would both be:

some_struct a;
another_struct b;

... and to the ass who assumed you don''t know structs just because you don''t know why someone would use an outdated C syntax when declaring them ... say something usefull for a change.

This topic is closed to new replies.

Advertisement