2

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

what is the difference between:

struct a{
...
}

and

typedef struct{
...
}  a;

?

2
  • 4
    exact duplicate: stackoverflow.com/questions/612328/… Commented Feb 7, 2011 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. Commented Feb 7, 2011 at 15:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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;.
é: You can also write class a if a was defined with struct, and vice versa.
0

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

1 Comment

@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.