Possible Duplicate:
Difference between 'struct' and 'typedef struct' in C++?
what is the difference between:
struct a{
...
}
and
typedef struct{
...
} a;
?
Possible Duplicate:
Difference between 'struct' and 'typedef struct' in C++?
what is the difference between:
struct a{
...
}
and
typedef struct{
...
} a;
?
In C++, there is no difference. In C, however, use of
struct a { ... };
Requires you to use the following to declare variables:
int main ( int, char ** )
{
struct a instance;
}
To avoid the redundant struct in variable declarations, use of the aforementioned typedef is required and allows you to use only the a instance; syntax
struct a instance; syntax is also valid in C++. If a was declared as class instead, you could also write class a instance;.class a if a was defined with struct, and vice versa.In the first to declare you must say struct a my_struct; in the latter you simply say a my_struct;
a), but the first is simpler and more idiomatic. You sometimes see the second version in C code where the first version defines a type that has to be referred asstruct ainstead ofa.