打字结构的Typdef指针与否? [英] Typdef pointers to structs or not?

查看:91
本文介绍了打字结构的Typdef指针与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以概述一下这样定义指向

结构的指针的优缺点吗?


typedef struct exp_ {

int val;

struct exp_ * child;

} * exp;


(这是直接从内存中所以它可能不是甚至编译。)


用例是AST表示,它大量使用指向

结构的指针(事实上IIRC它只使用指针,从不使用简单的结构)。

现在我没有使用typedef,因为我读了Linux内核编码

指南(作者认为)它混淆了什么是真的

还在继续。请注意,编译器中的几乎所有函数都将修改这些结构,因此整个typedef将位于.h文件中

或者需要一堆访问函数。

Could someone outline the pros and cons of typedefing pointers to
structs like this?

typedef struct exp_ {
int val;
struct exp_ *child;
} *exp;

(This is straight from memory so it might not even compile.)

The use case is AST representation which makes heavy use of pointers to
structs (in fact IIRC it only uses pointers, never plain structs).
Right now I don''t use typedefs since I read in the Linux kernel coding
guidelines that (the author believes) that it obfuscates what''s really
going on. Note that pretty much every function in the compiler will
modify these structs so either the whole typedef will be in a .h file
or a bunch of access functions are needed.

推荐答案



Johan Tibell写道:

Johan Tibell wrote:

有人可以勾勒出类似于这样的
结构类型指针的优缺点吗?


typedef struct exp_ {

int val;

struct exp_ * child;

} * exp;
Could someone outline the pros and cons of typedefing pointers to
structs like this?

typedef struct exp_ {
int val;
struct exp_ *child;
} *exp;



专业人士:


允许程序员编写使用

结构的代码保存一些按键。


缺点:


导致维护程序员在实现exp时会出现一些恶化是一个指针,

如果

对象被明确宣布,那将是显而易见的。


就个人而言,我认为是typedef在这些情况下没有帮助。

pros:

allows a programmer that is writing code that uses
the structure to save a few keystrokes.

cons:

causes the maintenance programmer some
aggravation upon realization that an exp is a pointer,
which would have been obvious had the
objects been declared explictly.

Personally, I think the typedef is not helpful in these cases.


Johan Tibell写道:
Johan Tibell wrote:

有人可以概述这种类型的指针的优缺点

这样的结构?


typedef struct exp_ {

int val;

struct exp_ * child;

} * exp;


(这是直接来自内存,因此它甚至可能无法编译。)


用例是AST表示,它大量使用指向

结构的指针(在f中)行为IIRC它只使用指针,从不使用简单的结构。

现在我不使用typedef,因为我读了Linux内核编码

指南(作者)相信)它混淆了真正的b
。请注意,编译器中的几乎所有函数都将修改这些结构,因此整个typedef将位于.h文件中

或者需要一堆访问函数。
Could someone outline the pros and cons of typedefing pointers to
structs like this?

typedef struct exp_ {
int val;
struct exp_ *child;
} *exp;

(This is straight from memory so it might not even compile.)

The use case is AST representation which makes heavy use of pointers to
structs (in fact IIRC it only uses pointers, never plain structs).
Right now I don''t use typedefs since I read in the Linux kernel coding
guidelines that (the author believes) that it obfuscates what''s really
going on. Note that pretty much every function in the compiler will
modify these structs so either the whole typedef will be in a .h file
or a bunch of access functions are needed.



缺点:

一些C程序员不喜欢它

它很难分辨是否有东西是指针


优点:

更容易被视为纯粹的抽象类型


For我,(亲)是一个明智的选择。其他情境中的其他程序员

可能会有不同的感受。


在你的特殊情况下 - 表达树 - 我有

事实typedefed pointers,完全是抽象类型的原因:

我后来更改了表示,以便Exp不是
a指针(和IIRC,后来改了回来)。在整个地方流浪了将会变得相当棘手。


[在任何情况下都可以通过函数访问这些字段 - -might-be-macros,

so -vs。 vs getWossname不是问题。]


-

Chris" seeker" Dollin

摇滚不是事实。岩石是一块岩石。

Cons:
some C programmers don''t like it
it''s harder to tell whether something is a pointer or not

Pros:
easier to treat as a purely abstract type

For me, the (pro) is a no-brainer. Other programmers in other contexts
may reasonably feel differently.

In your particular circumstances -- expression trees -- I have in
fact "typedefed pointers", for exactly the abstract type reason:
I changed the representation later so that an Exp wasn''t
a pointer (and, IIRC, changed it back later later). Having stray
*''s all over the place would have made that rather trickier.

[Access to the fields was in any case via functions-which-might-be-macros,
so -vs . vs getWossname wasn''t an issue.]

--
Chris "seeker" Dollin
A rock is not a fact. A rock is a rock.




文章< 11 ************ **********@h48g2000cwc.googlegroups .com> ;," Johan Tibell" < jo ********** @ gmail.comwrites:

In article <11**********************@h48g2000cwc.googlegroups .com>, "Johan Tibell" <jo**********@gmail.comwrites:

有人可以概述一下typedefing指针的优点和缺点

结构是这样的吗?


typedef struct exp_ {

int val;

struct exp_ * child;

} * exp;
Could someone outline the pros and cons of typedefing pointers to
structs like this?

typedef struct exp_ {
int val;
struct exp_ *child;
} *exp;



这是一个宗教问题,在comp.lang.c上已被激烈争论过多次

次。 GIYF;例如:

http://groups.google.com/group/comp....5d2682bb789321


http://tinyurl.com/kauj6


(我建议从皮特的第一条消息开始,这是讨论风格问题的开始。
http://groups.google.com/group/comp....0c9baab59f2511


http://tinyurl.com/ky7st


(样式讨论从Keith的第一条消息开始。)

就个人而言,我不喜欢typedef,除非有必要(提取

某些类型的参数来自一个可变参数列表;我不能
的不良做法,这是C'创建新类型的实际工具,

既可见又抽象。而且,就我而言,伪装指针 -

性质的typedef更糟糕。


在这种意见中我加入了某种程度,由Keith Thompson和

Chris Torek。但是其他人不同意,正如你在讨论中看到的那样。

我引用。


-

Michael Wojcik mi ************ @ microfocus.com

不要测试零件,因为这可能会损害敏感的细木工。

遭遇困难的人应立即放弃企业。 - Chris Ware

This is a religious issue which has been hotly debated a number of
times on comp.lang.c. GIYF; for example:

http://groups.google.com/group/comp....5d2682bb789321
or
http://tinyurl.com/kauj6

(I suggest beginning with the first message from pete, which is
the start of the discussion of the style issue.)
http://groups.google.com/group/comp....0c9baab59f2511
or
http://tinyurl.com/ky7st

(The style discussion begins with the first message from Keith.)
Personally, I dislike typedef except when it is necessary (to extract
certain types of argument from a variadic argument list; I can''t
think of any other cases offhand). I think they serve no useful
purpose and encourage certain poor practices that are prevented by
using "struct", which is C''s actual facility for creating new types,
both visible and abstract. And typedefs which disguise pointer-
nature are even worse, as far as I''m concerned.

In this opinion I am joined, to some degree, by Keith Thompson and
Chris Torek. But others disagree, as you can see in the discussions
I cited.

--
Michael Wojcik mi************@microfocus.com

Do not "test" parts, as this may compromise sensitive joinery. Those who
suffer difficulty should abandon the enterprise immediately. -- Chris Ware


这篇关于打字结构的Typdef指针与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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