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

问题描述

这两种方法之间是否有任何功能差异(或任何其他理由更喜欢

一个):


typedef struct mystruct {

int a;

int b;

} mystruct;


struct mystruct {

int a;

int b;

};

typedef struct mystruct mystruct;

Is there any functional difference (or any other reason to prefer
one over the other) between these two methods:

typedef struct mystruct {
int a;
int b;
} mystruct;

struct mystruct {
int a;
int b;
};
typedef struct mystruct mystruct;

推荐答案




不,但有时候有理由这样做:


typedef struct mystruct mystruct;


struct mystruct {

int a;

int b;

};


因为你可以使用类型名称mystruct在结构的

定义之前和之前,如果你的结构相互指向

就很有用。


- 理查德


-

考虑到需要多达32个字符

字母" - X3.4,1963。

No, but there is sometimes reason to do it the other way round:

typedef struct mystruct mystruct;

struct mystruct {
int a;
int b;
};

Because you can then use the type name "mystruct" in and before the
definition of the struct, which is useful if you have structures that
point to each other.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.




我不相信有任何真正的区别。请注意,在这两种情况下,

在其自己的定义中对类型的任何引用(例如,如果

mystruct包含指向mystruct的指针)必须使用名称' 'struct

mystruct'',因为typedef名称还不存在。


您甚至可以删除标签,只需声明


typedef struct {

int a;

int b;

} mystruct;


因为你从不使用struct标签名称。但是如果你将mystruct指针添加为成员,你将需要

标签。


使用相同的名称作为struct标签和typedef

完全合法,但可能会在某些IDE中引起问题。如果这导致您使用不同的名称,请选择一致的约定以避免

混淆。


理查德希思菲尔德争辩说第二种形式的风格

理由,因为自从宣布两个名字(''struct

mystruct''和''mystruct''),应该是两个声明。我会让他反驳我公然歪曲他实际说的话。 8-)}


其他人,包括我自己,认为在大多数情况下,

strutures的typedef是多余的。你的类型已经有一个非常好的

名字,''struct mystruct'';它不需要另一个。每次引用类型时使用''struct''

关键字会提醒读者

相关事实,即类型是结构类型。如果这个事实不是相关的,那么,如果你正在创建一个像FILE这样的不透明类型,那么

使用typedef是有道理的。


在这一点上,很多非常聪明的人都不同意我的意见,并且

相信这个类型的单字名称是值得的。
值得。


-

Keith Thompson(The_Other_Keith)< http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst> ;

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

- Antony Jay和Jonathan Lynn,是部长

I don''t believe there''s any real difference. Note that in both cases,
any reference to the type within its own definition (say, if a
mystruct contains a pointer to a mystruct) has to use the name ''struct
mystruct'', since the typedef name doesn''t exist yet.

You could even drop the tag, and just declare

typedef struct {
int a;
int b;
} mystruct;

since you never use the struct tag name anyway. But you''ll need the
tag if you ever add a mystruct pointer as a member.

Using the same name for the struct tag and for the typedef is
perfectly legal, but may cause problems in some IDEs. If this induces
you to use different names, pick a consistent convention to avoid
confusion.

Richard Heathfield has argued for the second form on stylistic
grounds, on the basis that since two names are being declared (''struct
mystruct'' and ''mystruct''), there should be two declarations. I''ll let
him refute my blatant misrepresentation of what he actually said. 8-)}

Others, myself included, have argued that in most cases typedefs for
strutures are superfluous. Your type already has a perfectly good
name, ''struct mystruct''; it doesn''t need another. Using the ''struct''
keyword every time you refer to the type reminds the reader of the
relevant fact that the type is a structure type. If this fact is not
relevant, i.e., if you''re creating an opaque type like FILE, then
using a typedef makes sense.

Plenty of very smart people disagree with me on this point, and
believe instead that having a one-word name for the type is
worthwhile.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于typedefing一个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 03:32