简短的问题
是否有适当或首选的方式来使用 C 中的structs
和类型定义enums
?
背景
我一直在研究一个代码库,该代码库有几个人/公司在研究它,我遇到了不同的typedefs
. 根据维基百科,它们似乎都具有相同的功能。我想我想了解它们之间是否存在细微的差异或陷阱。相同的用法和问题适用于typedef
ing enum
。
// I tend to use this if I need to create an
// alias outside the defining module.
typedef struct obj obj_t;
struct obj
{
UINT8 val;
};
// I started to use this format to alias objects
// within the same module (both private and
// public definitons). This is also how
// several colleagues use them.
typedef struct obj_t
{
UINT8 val;
}obj_t;
// This is how I now typically format the aliases
// for both public and private definitions.
typedef struct
{
UINT8 val;
}obj_t;
// I just ran into this one. While makes sense
// how it works, it wasn't inherently better or
// worse then the formats above. Note the different
// container names.
typedef struct obj_t
{
UINT8 val;
}obj;