Archived

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

Typedef'ing structs, why don't I understand why? =P

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

Why exactly do people typedef structs? What does it accomplish. You see a lot of: typedef struct struct_t { // Stuff } struct_s What exactly is the point of the typedef?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it is so that instead of writing

struct struct_t mystruct
you can just write
stuct_s mystruct

I don''t think this is needed in C++ though, just C

Share this post


Link to post
Share on other sites
Really? Then tell my why VC++ 7.1 told me otherwise


struct tagSoundFile
{
ALuint sourceid;
ALuint bufferid;

}SoundFile;

SoundFile test;



Error message

Missing ; before identifier test.

with a typedef, no error.





[edited by - Maega on January 6, 2004 1:48:23 AM]

Share this post


Link to post
Share on other sites
Thats not my point.

I was going by the example put up by the original poster .

If you do use a

struct something
{

}SomethingElse

You do need a typedef to refer to it as SomethingElse.



[edited by - Maega on January 6, 2004 2:04:50 AM]

Share this post


Link to post
Share on other sites
... in that example SomethingElse is an object of a struct - not a struct, so its no surprise that your average compiler has a hard time swallowing it.

[edit]That was a reply to Maega[/edit]

[edited by - emileej on January 6, 2004 2:14:26 AM]

Share this post


Link to post
Share on other sites
I use typedef for really ugly templates:

typedef vector<vector<pair<string,int> > > SomeType;
typedef SomeType::iterator SomeTypeIterator;

Because I don''t want to look at that big thing all the time.

Share this post


Link to post
Share on other sites
I don't see any obvious reasons for typedef-ing a struct. After all, the name of the stuct defines the user type. typedef-ing it seems redundant to me. There may be a reason someone might want to typedef it, but I haven't come across a reason for it. Now Zipster points out some common uses for typedef that I also use.

EDIT: What I said references C++.

[edited by - nervo on January 6, 2004 5:05:21 AM]

Share this post


Link to post
Share on other sites
typedef struct a {} b;
Both struct a and b are typenames.

typedef struct {} b;
b is a typename.

struct a {};
struct a is a typename.

struct {} b;
b is an instance of a nameless struct.

struct a {} b;
b is an instance of struct a.

Share this post


Link to post
Share on other sites
quote:
Original post by Maega
Thats not my point.

I was going by the example put up by the original poster .

If you do use a

struct something
{

}SomethingElse

You do need a typedef to refer to it as SomethingElse.

Of course. Because that''s creating an instance of a struct something named SomethingElse.

But that isn''t what the original poster was doing. The original poster was making a typedef, not an instance. The typedef is needed so that instead of having to use the type name "struct struct_t" one can instead use the name "struct_s".

In C++ (and C99 IIRC) that''s unnecessary -- one can simply declare the type as struct_s in the first place; one need not prepend "struct", though one can if one wants to.

Share this post


Link to post
Share on other sites