In typedef and struct namespaces in C vs C++, one of the comments seems to imply that exposing some struct foo is preferable to using typedef along the lines of…
typedef struct foo foo;
…and then using foo instead of struct foo throughout the API.
Are there any downsides to the latter variant?
It depends how much you like the word
struct. If you feel your program will be made clearer by a liberal sprinkling ofstruct thatandstruct tother(you can’t havestruct thisin C++, of course), then by all means use thestructversion.Personally, I don’t think that repeating
structprovides any benefit and I’m happy to use just thetypedefname. And because C++ effectively provides thetypedef struct xyz xyz;declaration automatically (it isn’t quite accurate, not least because you can explicitly write that in C++, but it is close enough that you probably don’t have to worry about it), I think it makes perfect sense to use the same in C. The C compiler is happy with it, so I normally usetypedef struct tag tag;and then usetagandtag *where needed.For an alternative but wholly tenable view, read the Linux kernel coding style guide.
Note that C2011 allows you to redefine a
typedefas long as it aliases the same type:Contrast with C99 where this was not possible:
ISO/IEC 9899:1999 §6.7 Declarations
This simplifies the creation of type definitions as long as you’re consistent (but only if you have a sufficiently compatible C2011 compiler on each platform of relevance to you).