2

Possible Duplicate:
Difference between 'struct' and 'typedef struct' in C++?

what is the difference between:

struct a{
...
}

and

typedef struct{
...
}  a;

?

Community
  • 1
  • 1
lital maatuk
  • 5,921
  • 20
  • 57
  • 79
  • 4
    exact duplicate: http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – badgerr Feb 07 '11 at 15:34
  • I don't think there is one (both define a type named `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 as `struct a` instead of `a`. – Philipp Feb 07 '11 at 15:34

2 Answers2

2

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

André Caron
  • 44,541
  • 12
  • 67
  • 125
  • Note that the `struct a instance;` syntax is also valid in C++. If `a` was declared as `class` instead, you could also write `class a instance;`. – André Caron Feb 07 '11 at 15:35
  • é: You can also write `class a` if `a` was defined with `struct`, and vice versa. – Philipp Feb 07 '11 at 15:37
0

In the first to declare you must say struct a my_struct; in the latter you simply say a my_struct;

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • @delnan : You are correct, I've been doing WAY too much C recently and I think things like this have probably sneaked their way into my C++ code. – Jesus Ramos Feb 07 '11 at 15:48