C语言 在 C 中使用 STRUCT 时的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5558994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming convention when using STRUCT in C
提问by Dean
I'm learning C and find someone defined a struct, its struct name has _ in front of it. This is my first time seen it, can someone tell me a little bit more about it? Why someone would use _aStructName instead of aStructName, what are the benefits?
我正在学习 C 并发现有人定义了一个结构,它的结构名称前面有 _。这是我第一次看到它,有人可以告诉我更多关于它的信息吗?为什么有人会使用 _aStructName 而不是 aStructName,有什么好处?
struct _huffmanNode {
int value;
uint32_t frequency;
int hasChild;
struct _huffmanNode *child[2];
struct _huffmanNode *next;
};
similarly I find someone using this kind of naming convention in the following code:
同样,我发现有人在以下代码中使用这种命名约定:
typedef struct HuffCode_ {
unsigned char used;
unsigned short code;
unsigned char size;
} HuffCode;
回答by Fred Foo
There is no benefit in user code, it's just ugly. In the second example, the HuffCode_isn't even necessary since the structtype is already named by the typedef.
用户代码没有任何好处,它只是丑陋。在第二个示例中,HuffCode_甚至不需要 ,因为struct类型已经由typedef.
The only places where this can be useful are:
唯一有用的地方是:
- When
StructNameis already in use,StructName_gives a different name (but you should really come up with a better name). - Identifiers in the C standard library that are not defined by the standard shouldn't conflict with user code identifiers. Therefore, C library writers use the
_prefix in the hopes that users will not use that. Unfortunately, some users do. - In very old compilers, it may be useful to give the
structa different name than is used in thetypedef. You need both thetypedefand the other name if you're building a linked structure (example).
- When
StructNameis already in use,StructName_给出一个不同的名字(但你真的应该想出一个更好的名字)。 - C 标准库中未由标准定义的标识符不应与用户代码标识符冲突。因此,C 库编写者使用
_前缀是希望用户不会使用它。不幸的是,有些用户这样做了。 - 在非常旧的编译器中,给出
struct一个不同于typedef.typedef如果您正在构建链接结构(示例),则需要同时使用 the和另一个名称。
回答by Jeremy West
I think this is done mostly because of the very mistaken idea that a struct and a type cannot have the same name. In other words, that somehow
我认为这样做主要是因为一个非常错误的想法,即结构和类型不能具有相同的名称。换句话说,不知何故
struct MyStruct;
typedef struct MyStruct MyStruct;
will collide in strange ways because the structand the typedefhave the same name. This is wrong in C. The name of a struct is considered a tag, whereas a typedef creates a type name. These live in two different namespaces and will not collide. In my opinion, it makes a lot more sense for the tag of the struct to be the same as the typedef, assuming you use a typedef at all. In C, you must always reference a struct with the struct keyword. For example, if you define
会以奇怪的方式碰撞,因为struct和typedef具有相同的名称。这在 C 中是错误的。结构的名称被认为是一个标签,而 typedef 创建一个类型名称。它们位于两个不同的命名空间中,不会发生冲突。在我看来,假设您完全使用 typedef,结构的标记与 typedef 相同更有意义。在 C 中,您必须始终使用 struct 关键字来引用结构。例如,如果您定义
struct MyStruct;
结构我的结构;
but do not make a typedef, then the following is invalid:
但不要进行 typedef,则以下内容无效:
void myfunction()
{
MyStruct var; /* compiler error: MyStruct is not defined. */
struct MyStruct var2; /* this is OK, MyStruct is a valid name for a struct. */
}
If you want to define variables (or arguments) of type MyStructin C, you mustprovide a typedef:
如果要MyStruct在 C 中定义类型的变量(或参数),则必须提供 typedef:
typedef struct MyStruct MyStruct;
void myfunction2()
{
MyStruct var; /* this is ok now */
struct MyStruct var2; /* this is still ok too */
}
In this second example, var, and var2have the same type, although this isn't obvious. Indeed, if the typedef were changed, they would no longer have the same type. Beware of this! It can cause some interesting bugs if type definitions change.
在第二个示例中var, 和var2具有相同的类型,尽管这并不明显。事实上,如果 typedef 被更改,它们将不再具有相同的类型。小心这个!如果类型定义发生变化,它可能会导致一些有趣的错误。
In C++, a struct definition essentially creates an implicit typedefso that both of the above code snippets compile. There is, in C++, essentially no difference between a struct name (or tag) and a type name. Which is, in my opinion, another great reason to name the two the same way, especially if you anticipate that your C module might be used by some C++ code at some point.
在 C++ 中,结构定义本质上创建了一个隐式,typedef以便上述两个代码片段都能编译。在 C++ 中,结构名称(或标记)和类型名称之间基本上没有区别。在我看来,这是以相同方式命名两者的另一个重要原因,特别是如果您预计您的 C 模块可能会在某些时候被某些 C++ 代码使用。
回答by Chris Eberle
A lot of the time in straight C, people like to typedef their structs so that they don't look so ugly. So they name the struct itself something ugly, and the typedef something clean. Usually the convention I've seen in the GNU world is:
很多时候在直接的 C 中,人们喜欢对他们的结构进行 typedef,这样它们看起来就不会那么难看。所以他们将结构本身命名为丑陋的东西,而将 typedef 命名为干净的东西。通常我在 GNU 世界中看到的约定是:
typedef struct mytype_t
{
int field;
char field2;
} mytype;
回答by Richard Schneider
I use a variant of the second example:
我使用第二个示例的变体:
typedef struct huffcode {
... } HuffCode;

