【发布时间】:2013-05-14 10:18:51
【问题描述】:
小问题
是否有适当或首选的方式在 C 中使用 structs 和 enums 的 typedef?
背景
我一直在研究一个代码库,该代码库有几个人/公司在研究它,我遇到了typedefs 的不同实现。根据Wikipedia,它们似乎都具有相同的功能。我想我想了解它们之间是否存在细微的差异或陷阱。相同的用法和问题适用于 typedef 和 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;
【问题讨论】:
-
根据this page,所有以
_t结尾的名称都是保留的,因此这可能不是最好的代码。
标签: c coding-style standards