Possible Duplicates:
Purpose of struct, typedef struct, in C++
typedef struct vs struct definitions
In code that I am maintaining I often see the following:
typedef enum { blah, blah } Foo;
typedef struct { blah blah } Bar;
Instead of:
enum Foo { blah, blah };
struct Bar { blah blah };
I always use the latter, and this is the first time I am seeing the former. So the question is why would one use one style over the other. Any benefits? Also are they functionally identical? I believe they are but am not 100% sure.
In C++ this doesn’t matter.
In C,
structs,enums, andunions were in a different “namespace”, meaning that their names could conflict with variable names. If you saySo you could say something like
and that would mean that
struct Sis the data type, andSis the variable name. You couldn’t sayin C if
Swas astruct(and not a type name), so people just usedtypedefto avoid sayingstructall the time.