3 ms·
> Wrap your structs in a typedef I tend to disagree. Unless you are providing an API for third-party use and want to make your data types opaque to callers, do
by bluetomcat 5y ago
> Wrap your structs in a typedef
I tend to disagree. Unless you are providing an API for third-party use and want to make your data types opaque to callers, do not ever use typedef with structs and pointers. A declaration like "struct foo *f" tells you two things - f itself is a pointer, and the pointed-to value is not a scalar (i.e. has named members). The "_t" suffix idiom is predominantly used by the standard library for integral scalars with certain properties and semantics, e.g. pid_t, uid_t, gid_t, time_t.
- visualradio 5y agoIf programmers do want to wrap structs in a typedef without using the "_t" suffix, the Vulkan API appears to have settled on upper-camel case: typedef struct VkMemoryBarrier { VkStructureType sType; const void* pNext; VkAccessFlags srcAccessMask; VkAccessFlags dstAccessMask; } VkMemoryBarrier;
- jimbob45 5y agoAgreed. If you typedef your struct, I can’t know if you wrapped it in a weird macro or if your data type is actually a renamed primitive. Once I gain familiarity with your codebase, it won’t matter, but it otherwise unnecessarily raises the barrier of entry. That said, typedefs/macros may be essential for portability concerns.
- Gibbon1 5y ago> can’t know if you wrapped it in a weird macro The solution to that problem is don't do that.
- ynfnehf 5y agoThe _t suffix is also reserved by posix. (And by the C standard if the name starts with int or unit.) I agree with just using struct foo, as does the Linux style guide. (Edit: I just noticed the page mentions this, but it is perhaps useful to reiterate.)
- mlex 5y agoSomewhat related; I feel like C reserves a lot of names that though different standards, is there a good list of all of those so I know to avoid them? (e.g. underscore prefixes)
- zabzonk 5y agoThis part of the GCC docs covers all of them, AFAIK: https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html https://www.gnu.org/software/libc/manual/html_node/Reserved-...
- IanGabes 5y agoHuh. This is interesting, I can empathize with both sides of the argument. Are C text editors not able to take care of syntax highlighting the differences between a typedef'd scalar and a typedef'd struct? Colouring the type one way or another I figure would be sufficient.