Jump to content
  • Advertisement
Sign in to follow this  
orcfan32

typedef structure?

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

I've been looking for info about the "typedef struct" in C++, but none of the articles seem to explain it in a way to understand it. Is it the same as a regular struct? What differences does it have? (If looked all over Google and MSDN)

Share this post


Link to post
Share on other sites
Advertisement
A long time ago, in a galaxy far, far away...

C compilers didn't automatically generate an identifier when structs were declared.

This means:

struct Bob { ... };
Bob b;


would throw a compile-time error, because Bob isn't registered as a valid identifer. There were two ways around this:

// The crappy way (prefix every instanciation declaration with 'struct'):
struct Bob b;

// The better way (typedef the struct):
typedef struct Bob_ { ... } Bob;
Bob b; // this now works properly, thanks to the typedef


Please do not do this in your C++ code though! Like SiCrane said, modern compilers don't have this problem!

Share this post


Link to post
Share on other sites
If you define a struct in C like this:

struct MyStruct { ... };

and you refer to that type, you always have to add the 'struct' keyword in front of it. If you typedef it like this:

typedef struct MyStruct { ... } MyStruct;

then you can also refer to the type without having to type the 'struct' keyword.

In C++ you don't need this, since the first version already allows you to refer to the type by its name (without 'struct').


Edit:
Seems nilkn was faster.

Share this post


Link to post
Share on other sites
Quote:
Original post by nilkn
A long time ago, in a galaxy far, far away...

C compilers didn't automatically generate an identifier when structs were declared.

Please note that C compilers still doesn't generate type tags for structs. It's a difference between C and C++, not a problem that has been fixed in "modern compilers".

Share this post


Link to post
Share on other sites
Sorry for hijacking this slightly but I have a question. I have been using structs by putting struct infrount of them every time which is hastle. So I tried the typedef way but if theres one in the actual structure it doesn't work. E.g. in a linked list of bob structures you would have a bob pointer for the next in the list inside the bob structure. How would I get around that?

Share this post


Link to post
Share on other sites
You have to use the struct tag to declare pointers to the struct type inside the struct definition. e.g.:

typedef struct tagNode {
struct tagNode * next;
struct tagNode * prev;
} Node;


edit: again, only do this if you are using C. In C++ should be avoided.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!