关于结构声明的问题 [英] Question about a struct declaration

查看:92
本文介绍了关于结构声明的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中,如果我想声明一个结构,有3种方法可以这样做:


1:struct dummy {...};

2:typedef struct {...} dummy;

3:typedef struct _dummy {...} dummy;


通常我用的是第一个声明我的结构的方法,但在许多开放的源项目(如libxml2)中,使用了第三种方法。我只是对这个差异感到好奇。有人会告诉我建议使用哪种方法

,为什么?提前致谢。


郑忠

In C, if i want to declare a struct, there are 3 methods to do so:

1: struct dummy { ... };
2: typedef struct { ... } dummy;
3: typedef struct _dummy { ... } dummy;

Usually i use the first method to declare my structs, but in many open
source projects (such as libxml2), the third method is used. I am just
curious about the difference.. Would somebody tell me which method is
recommended, and why? Thanks in advance.

ZHENG Zhong

推荐答案

heavyz< he ******* *@gmail.com写道:
heavyz <he********@gmail.comwrote:

在C中,如果我想声明一个结构,有3种方法可以这样做:


1:struct dummy {...};

2:typedef struct {...} dummy;

3:typedef struct _dummy {...}假;
In C, if i want to declare a struct, there are 3 methods to do so:

1: struct dummy { ... };
2: typedef struct { ... } dummy;
3: typedef struct _dummy { ... } dummy;



^^^^^^

这是不明智的。以_和小写字母开头的标识符只有编译器本身才允许使用
,不适合你,除非它们是在块范围内声明的
;但大多数结构类型(a.o.t. struct _objects_)

未在块范围内声明,因为它们对文件

范围更有用。 (以_和大写字母开头的标识符或者带__的
不允许我们用户,完全停止。)

不同的标识符是不必要的,无论如何。对于结构标识符和普通标识符,C具有单独的

名称空间,因此struct dummy

和plaindummy不要碰撞,这意味着


typedef struct dummy {...}虚拟;


是(当充实时)一个完全有效的声明,它包含一个

结构类型和一个typedef'的别名;和IYAM,如果你想要或需要

来同时使用结构名称和typedef,那么同时使用它们同样更清楚。

^^^^^^
This is unwise. Identifiers beginning with _ and a lower-case letter are
only allowed for the compiler itself, not for you, except when they''re
declared at block scope; but most struct types (a.o.t. struct _objects_)
are not declared at block scope, because they''re more useful with file
scope. (And identifiers beginning with _ and an upper-case letter or
with __ are not allowed for us mere users, full stop.)
The different identifiers are unnecesary, anyway. C has separate
namespaces for struct identifiers and for normal ones, so "struct dummy"
and plain "dummy" do not clash, which means that

typedef struct dummy { ... } dummy;

is (when fleshed out, of course) a perfectly valid declaration of both a
struct type and a typedef''ed alias for it; and IYAM, if you want or need
to use both a struct name and a typedef for it, having them both the
same is clearer, as well.


通常我使用第一种方法来声明我的结构,但是在许多开放的
源项目(例如libxml2)中,使用了第三种方法。我只是对这个差异感到好奇。有人会告诉我建议使用哪种方法

,为什么?提前致谢。
Usually i use the first method to declare my structs, but in many open
source projects (such as libxml2), the third method is used. I am just
curious about the difference.. Would somebody tell me which method is
recommended, and why? Thanks in advance.



在大多数方面,它都是品味问题,但IMO,除非你有好的

理由不要,第一个是最好的,因为它是最简单的。

如果你正在创建一个抽象的数据类型

库并且不想告诉你用户如何实施你的AD $>
ADT;然后你将结构定义保存在ADT库代码中

本身,并且只将typedef放在相关的标题中。


Richard

In most aspects it''s a matter of taste, but IMO, unless you have good
reason not to, the first one is best, because it''s simplest.
One good reason not to would be if you''re creating an abstract data type
library and do not want to tell your users how you''re implementing your
ADT; you''d then keep your struct definition in the ADT library code
itself, and only put the typedef in the associated header.

Richard


heavyz写道:
heavyz wrote:

在C中,如果我想声明一个结构,有3种方法可以这样做:


1:struct dummy {...};

2:typedef struct {...} dummy;

3 :typedef struct _dummy {...} dummy;


通常我使用第一种方法来声明我的结构,但在许多开放的
源项目中(如作为libxml2),使用第三种方法。我只是对这个差异感到好奇。有人会告诉我建议使用哪种方法

,为什么?提前致谢。
In C, if i want to declare a struct, there are 3 methods to do so:

1: struct dummy { ... };
2: typedef struct { ... } dummy;
3: typedef struct _dummy { ... } dummy;

Usually i use the first method to declare my structs, but in many open
source projects (such as libxml2), the third method is used. I am just
curious about the difference.. Would somebody tell me which method is
recommended, and why? Thanks in advance.



#1是结构类型定义。这是足够的,很少需要更多的b $ b。代码中的

类型是struct dummy,除非后来有一个

typedef。


#2和#3用于为结构提供别名。

在#2中,没有别名标记,因此类型必须始终为虚拟。

这隐藏了这样一个事实:它是一个结构,可能是一个坏主意。

在#3中,你为类型创建了两个名称,''struct _dummy''和''dummy''。 br />
使用以下划线开头的标识符是个坏主意,除非你完全熟悉这些名称的确切限制。你要问这个问题,这个问题表明你不是。所以#3可以是

完成(注意没有下划线)

3a。 typedef struct dummy {/ * ... * /} dummy;

或两部分;

3b。 struct dummy {/ * ... * /}; / * structure-type-definition * /

typedef struct dummy dummy; / * typedef创建别名* /


我几乎在所有情况下都喜欢#1。


#2可能比你的#3更好我的#3a或#3b,因为有一个

指向没有两种识别类型的方法。 #3,#3a和

#3b,#3是最差的。 #3b有实际用途:即在实现文件中提供一个

结构类型定义,并在标题中提供一个

typedef,你希望这个类型不透明用户,

他只会通过指针来引用对象。


决定程序员如何引用结构。如果你想要'b $ b'他使用''struct dummy'',请使用#1。如果你想让他使用''dummy'',请使用

#2。如果你想通过

类型''dummy *'的指针引用opaque数据类型,请使用#3b。

#1 is a structure-type-definition. It is sufficient and rarely is more
needed. The
type thoughout the code is ''struct dummy'' unless there is a later
typedef.

#2 and #3 are used to provide an alias for the struct.
In #2, there is no alias tag and so the type must be ''dummy'' throughout.
This hides the fact that it is a struct and is probably a bad idea.
In #3, you create two names for the type, ''struct _dummy'' and ''dummy''.
It is bad idea to use identifiers that begin with underscores unless you
are thoroughly familiar with the exact restrictions on such names. That
you are asking this question suggests that you are not. So #3 could be
done as either (note the absence of the underscore)
3a. typedef struct dummy { /* ... */ } dummy;
or in two parts;
3b. struct dummy { /* ... */ }; /* structure-type-definition */
typedef struct dummy dummy; /* typedef creating alias */

I prefer #1 in almost all situations.

#2 is probably better than your #3 or my #3a or #3b, since there is a
point to not having two ways of identifying the type. Of #3, #3a, and
#3b, #3 is the worst. #3b has real uses: namely to provide a
structure-type-definition in implementation files and providing a
typedef in a header where you want to make the type opaque to the user,
and he will refer to objects only through pointers to them.

Decide how you want the programmer to refer to a struct. If you want
him to use ''struct dummy'', use #1. If you want him to use ''dummy'', use
#2. If you want opaque datatypes referred to though pointers of the
type ''dummy *'', use #3b.


heavyz< he ******** @ gmail.comwrites:
heavyz <he********@gmail.comwrites:

在C中,如果我想声明一个结构,有3种方法可以做所以:


1:struct dummy {...};

2:typedef struct {...} dummy;

3:typedef struct _dummy {...} dummy;


通常我使用第一种方法来声明我的结构,但在许多开放的
源项目中(例如libxml2),使用第三种方法。我只是

对这种差异感到好奇..
In C, if i want to declare a struct, there are 3 methods to do so:

1: struct dummy { ... };
2: typedef struct { ... } dummy;
3: typedef struct _dummy { ... } dummy;

Usually i use the first method to declare my structs, but in many open
source projects (such as libxml2), the third method is used. I am just
curious about the difference..



方法2的缺点是在<之前没有该类型的名称br />
你到达声明的末尾,所以你不能宣布

类型的成员dummy * (例如,对于树或链表中的节点)。


方法3,正如Richard Bos指出的那样,侵犯了

的实现''名称空间。这可以通过对typedef和struct标签使用相同的

标识符来修复:


typedef struct dummy {...} dummy;


或者如果你不想因为某些原因重新使用相同的

标识符而使用其他约定(可能是因为你的

开发环境):


typedef struct dummy_s {...} dummy;

Method 2 has the disadvantage that there''s no name for the type until
you reach the end of the declaration, so you can''t declare a member of
type "dummy*" (e.g., for a node in a tree or linked list).

Method 3, as Richard Bos pointed out, infringes on the
implementation''s name space. This can be fixed by using the same
identifier for the typedef and the struct tag:

typedef struct dummy { ... } dummy;

or by using some other convention if you don''t like re-using the same
identifier for some reason (perhaps due to a limitation in your
development environment):

typedef struct dummy_s { ... } dummy;


有人会告诉我建议使用哪种方法
,为什么?提前致谢。
Would somebody tell me which method is
recommended, and why? Thanks in advance.



建议使用这三种方法。这取决于你问的是谁。这个

恰好是一个有争议的问题(但不是我们大多数人得到的问题。

太累了。)


就个人而言,我更喜欢方法1,除非有充分理由隐藏类型为结构的

事实。其他人会更喜欢方法2或3

,因为他们喜欢这个类型的单字名称,并且不要
感觉需要提醒它'每次他们提供它时都是一个结构。


如果您正在处理现有代码,请遵循其中使用的样式

代码;一致性比任何一种风格都要重要。

可能比另一种风格更重要。如果您正在编写自己的原始代码,请选择一种风格并坚持下去;你可以自由地遵循我的建议,

或那些可怜的误导灵魂的建议,他们不同意我的原因。
他们自己不可思议的原因。

-

Keith Thompson(The_Other_Keith)< ks *** @ mib.org>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

All three are recommended. It just depends on who you ask. This
happens to be a controversial issue (but not one that most of us get
too worked up about).

Personally, I prefer method 1 unless there''s a good reason to hide the
fact that the type is a struct. Others will prefer method 2 or 3
because they like to have a single-word name for the type and don''t
feel the need to be reminded that it''s a struct every time they
mention it.

If you''re working on existing code, follow the style used in that
code; consistency is more important than any small benefit one style
might have over another. If you''re writing your own original code,
pick a style and stick to it; you''re free to follow either my advice,
or the advice of those poor misguided souls who disagree with me for
their own unfathomable reasons.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于关于结构声明的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆