1

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

Is there a difference between

typedef struct{
....
} mystruct;

and

struct mystruct{
....
};

?

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625

4 Answers4

1

It's useless in C++. In C, it's because structs have their own namespace (you need to write struct T if you don't typedef to something else).

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
1

In C, the syntax to declare a struct is struct mystruct var;, so developers often typedef an anonymous struct to make declaring as simple as mystruct var;. C++ allows you to define structs without the struct keyword, so the typedef is used less often.

yan
  • 20,644
  • 3
  • 38
  • 48
0

There is no real difference in C++.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
0

This idiom is commonly used in C, where a struct variable would need to be declared as struct StructName myStruct, and StructName myStruct wouldn't work. It's not necessary in C++.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174