In my recent operating systems class we have a bunch of objects defined as such:
typedef struct someobj {
... stuff ...
} someobj_t;
Run Code Online (Sandbox Code Playgroud)
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?
虽然由您决定是使用typedef还是结构名称,但是有一个很好的理由不使用 typedef
以...结尾的名称_t
.所有这些名称都由POSIX保留以供实现使用,并且可以指特定于实现的类型,或者可以在POSIX的未来版本中标准化.不幸的是,许多图书馆作者忽视
我个人不喜欢不typedef
用于结构,但如果你选择使用它,至少要避免使用保留的命名空间.
假设我们正在询问下面的代码(typedef
在我写这个问题时没有问题,但我认为它应该在那里):
typedef struct someobj {
// content
} someobj_t;
Run Code Online (Sandbox Code Playgroud)
实际上,通常只需省略名称someobj
并使用someobj_t
一致就足够了.但是,当您希望结构引用自身时,就是这种情况:
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
Run Code Online (Sandbox Code Playgroud)