Jump to content
  • Advertisement
Sign in to follow this  
Mattman

structs and typedefs

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

It's been awhile since I've done some C programming (been on a Java kick lately), and I'm trying to remember why people sometimes create typedefs for structs like this...
typedef struct _MY_DATA_TYPE
{
    // structure definition here.
} MY_DATA_TYPE;
...instead of just creating a struct with that name in the first place, like this...
struct MY_DATA_TYPE
{
    // structure definition here.
};
Thanks!

Share this post


Link to post
Share on other sites
Advertisement
In C you must use a struct as follows:

// Define the struct
struct S
{
...
};

// Declare a variable
struct S a;


Because the second struct-word is annoying to type, one can use a typedef to obtain the following:

// Define the struct
struct S;

// Typedef
typdef struct S S;

// Declare a variable
S a;


And this can be combined into:

typdef struct S
{
...
} S;

S a;


In C++ this is unnecessary but it is done a lot by programmers coming from C. Greetz,

Illco

Share this post


Link to post
Share on other sites
Aaahhh, right, now I remember a point when first learning C that I had to use "struct S a;" everywhere to define a variable of that struct... thanks for refreshing my memory! :)

Share this post


Link to post
Share on other sites
In OSS, you'll find that they don't use typedef much, because the struct is used to remind them that a given identifier has member data. However, I learned that when they do, they tend to use the _t suffix on identifiers to indicate that its a typedef.

typedef struct S
{
...
} S_t;

S_t a;

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!