3

I was wondering why you would use the specifier typedef on the types struct and union, because aren't they somewhat used to define your own type anyways?

Thanks!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271

1 Answers1

2

Edit Reading the standard the 'typedef' isn't required in C++ and is probably there for backward compatibility. Also, with 'typedef' the code would compile in both C and C++ compilers without change or '#if define' around it so it's probably a legacy code support thing.

I think people do it to save on typing! We're a lazy lot afterall. So instead of:-

struct SomeStruct
{
  // some data
};

struct SomeStruct first_instance;
struct SomeStruct second_instance;

you'd have this instead:-

typedef struct typedefSomeStruct
{
  // some data
} SomeStruct;

SomeStruct first_instance;
SomeStruct second_instance;

So when you declare instances of the type you don't need to prefix the 'struct' keyword.

Skizz
  • 69,698
  • 10
  • 71
  • 108