I have tried to find out, how to name a struct and type correctly. I always found what not to do ("__name", "_Name"), so I always did it like this:
typedef struct something_t {
...
} something_t;
// or
typedef struct something_that_should_not_be_copied_t {
...
} something_that_should_not_be_copied_t[1];
But I think this is not fully correct, because _t
is reserved for type names, so "struct something_t" is not allowed. I also heard, that "struct something_s" is correct, but I have never seen it in any code base. So please, tell me, what is the correct way to do this in a normal program and in a library.
CodePudding user response:
When using typedef struct { } name;
, you don't even need to state a struct tag. This is only required if you are doing something special like a self-referencing struct or an implementation of "opaque types". So the most common solution would simply be to omit the struct tag.
struct tags and typedef
names exist in different name spaces, so you can name them the same if you fancy, as far as the C language is concerned.
There is as far as I know no convention for naming struct tags. However, some coding standards like the Linux kernel encourages struct tag
over typedef, for completely subjective reasons.
_t
at the end of a type is a common naming convention for types however. It is fine to use, although prohibited by the POSIX standard.
Depending on what kind of application you are writing, you could either follow things like the Linux kernel coding style or POSIX, or you can completely ignore them.