C Typedef and Struct Question
c, namespaces, struct, typedef
Solution
As far as which style is preferred, I prefer the 1st style (with the name in both the struct tag and as a typedef for the struct) simply because there are no drawbacks other than a few more characters in the source file. My IDE's struct snippet drops the name in both places, so I always get a typedef'ed struct name along with a struct tag name.
You get a few small benefits:
- the "`struct STRUCTNAME`" can be used to declare pointers to the struct within the struct
- you can get away with just using STRUCTNAME in either C or C++ code
- you prevent a potential (even if very rare in practice) oddity in C++ having to do with the struct name being used for another object without error or warning
But, if I happen to manually type in the struct definition, I'll often lazily neglect declaring one or the other name.
Problem
What's the difference between these two declarations, and is one preferred over the other? ``` typedef struct IOPORT { GPIO_TypeDef* port; u16 pin; } IOPORT; typedef struct { GPIO_TypeDef* port; u16 pin; } IOPORT; ```