In my recent operating systems class we have a bunch of objects defined as such:
typedef struct someobj {
... stuff ...
} someobj_t;
I know what that does just fine.
The question is that sometimes in the given support code the structs were refered to as struct someobj *some
, and sometimes as someobj_t *some
. Is there an actual / useful reason to refer to structs in these two different ways, or is just a stylistic difference?
While it's up to you whether you use a typedef or the struct name, there is a very good reason not to use typedef
names ending in _t
. All such names are reserved by POSIX for use by the implementation, and may refer to implementation-specific types or may be standardized in future versions of POSIX. Unfortunately many library authors ignore this.
Personally I prefer not to use typedef
for structures, but if you do choose to use it, at least avoid the reserved namespace.
Assuming we're asking about the following code (there's no typedef
in the question as I'm writing, but I'm assuming it was meant to be there):
typedef struct someobj {
// content
} someobj_t;
Actually usually it'd be sufficient to omit the name someobj
and use someobj_t
consistently. However there's that situation when you want the struct to refer to itself:
typedef struct someobj {
struct someobj* next;
// cannot say someobj_t* yet - typedef not complete
} someobj_t;
// and from now on, both struct someobj and`someobj_t are equivalent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With