1

Based on my little bit of learning typedef is used to create an alias. But I see code where the "alias" is the exact same name as the original. What is the use here?

e.g.

typedef struct Dog{
  bool hasLegs;
} Dog;
  • 3
    This is a C-ism/just plain C code. I've closed to a dupe that explains why this is done. In C++, this is totally unneeded and the class can just be written as `struct Dog { bool hasLegs; };` – NathanOliver May 11 '22 at 19:29
  • You do it in C so you don't have to write `struct Dog` everywhere and you can just go with `Dog`. It's just to make things easier. (I'm not sure of the use in C++ but I guess its similar) – Jakub Bednarski May 11 '22 at 19:31
  • @JakubBednarski Same use in C++, but C++ built this behaviour in by default making the `typedef` trick redundant. Still useful if you need to write interop code, though. – user4581301 May 11 '22 at 19:51
  • For this particular struct, in C all that would be needed (snoring the non-C `bool`) is `typedef struct { bool hasLegs; } Dog;`. – Pete Becker May 11 '22 at 19:54

0 Answers0