What's the difference

I have seen this "style" of coding in many places and have yet figured out why someone would use one "style" versus the other. I the best of my knowledge they do the same thing, but then again, we can learn something new every day. Anybody have a logical explanation?

Style 1:
1
2
3
4
5
6
7
typedef struct _structure
{
    //data here
    int a;
    int b;

} structure;


Style 2:
1
2
3
4
5
6
typedef struct structure
{
    //data here
    int a;
    int b;
};


Thanks


Last edited on
The "Style 1" is the recommended way. It declares a structure _structure and creates an alias structure for that enabling you to create objects without the struct keyword.

The "Style 2" also aims for the same. But it omits the alias name. It is not actually an error in a c++ compiler but you may get warings like the one below which is obtained from a vc++ 6 compiler.
warning C4091: 'typedef ' : ignored on left of 'struct structure' when no variable is declared


Even when you declare a structure without a typedef keyword, the vc++ compiler will let you create object without the "struct" keyword.
I think on Style 2 you should omit 'typedef'
1
2
3
4
5
6
struct structure
{
    //data here
    int a;
    int b;
};
Style 1 is used in C. I can't see any reason to ever use Style 2.
See http://www.cplusplus.com/forum/beginner/7141/
That explains a lot. Thanks
Topic archived. No new replies allowed.