Archived

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

typedef struct

This topic is 5586 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

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)

Share this post


Link to post
Share on other sites
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 use

Integers struct1, struct2;
//instead of

struct 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites