“ - >”的理由? [英] Justification for "->"?

查看:51
本文介绍了“ - >”的理由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速搜索这个群组及其常见问题解答,以及其他地方,没有

我满意地回答了这个问题。抱歉,如果我错过了一些明显的东西,无论是在文献中还是在我的推理中。


有人可以告诉我为什么 - >存在?编译器知道结构和指向结构的指针之间的差异

,为什么不能只是

让我写foo.bar在这两种情况下都没有回去和

当我后来决定我想要一个指针而不是一个值时,重写一些东西

,反之亦然?我经常改变主意,例如,一个函数

应该收到一个指针或者东西本身,而且温和地说

不方便更改引用
$ b的所有代码$ b成员。


有历史原因吗?我读过关于C历史的dmr文章,他提到了foo-> bar的时间。无论

是什么类型的foo,bar都可以使用只是描述一个偏移量和成员价值的类型

,但这让我感到困惑 - 是不允许的?

在两种不同的结构类型中使用相同的成员名称?但是即使原来的原因与此有关,为什么在类型系统变得强大之后该师保持



解决方案

在文章< 11 ********************** @ d30g2000prg.googlegroups .com>

< no ********** @ gmail.comwrote:


>有人可以告诉我原因吗? - >"存在?



历史,你猜测:


>编译器知道结构和结构之间的区别
指向一个结构的指针,所以为什么不能让我写一下
foo.bar在这两种情况下[...]?



有些语言可以做到这一点,包括一些调试器

说C的语言。 (特别是gdb)。与往常一样,C与众不同。 :-)


>我读了关于C的历史的dmr文章,他提到了foo-> bar的时间。无论foo的类型如何,bar都会起作用。只是描述了一个偏移量和成员值的类型,但这让我感到困惑 - 是不允许在两种不同的结构类型中使用相同的成员名称?



正确 - 一个不是;构造如:


struct S {int a,b; };

struct T {int c,a; };


创建了问题,因为名为a的成员必须指两个

" offset 0"和偏移2。同时(sizeof(int)为2,

因为这个史前C只在PDP-11上被发现)。


实际上,你甚至可以写:


struct {char lo,hi; };

int var = someval;

...

var.lo =''a'';


生成的机器代码类似于:


*(char *)& var =''a'';


adb调试器使用了这个。


>但即使最初的原因与此相关,为什么在类型系统变强后保留
分区?



一些源代码 - 例如adb调试器,也许是

Bourne shell - 取决于此。截至20世纪90年代初,一些

程序仍然使用cc -W编译,其中-W选项

*抑制*警告。 (当然,这不是* gcc,其中-W

*添加*警告。)


(我帮助重写了adb,作为UCB努力的一部分,BSD网络版通常可用,并删除

各种机器依赖。)

- -

In-Real-Life:风河系统Chris Torek

美国犹他州盐湖城(40°39.22''N,111°50.29''W )+1 801 277 2603

电子邮件:忘了它 http://web.torek.net/torek/index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。


Chris Torek写道:


文章< 11 ***************** *****@d30g2000prg.googlegroups .com>

< no ********** @ gmail.comwrote:


>有人可以告诉我为什么 - > "存在?



历史,你猜测:


>编译器知道结构和结构之间的区别
指向一个结构的指针,所以为什么不能让我写一下
foo.bar在这两种情况下[...]?



有些语言可以做到这一点,包括一些调试器

说C的语言。 (特别是gdb)。与往常一样,C与众不同。 :-)


>我读了关于C的历史的dmr文章,他提到了foo-> bar的时间。无论foo的类型如何,bar都会起作用。只是描述了一个偏移量和成员值的类型,但这让我感到困惑 - 是不允许在两种不同的结构类型中使用相同的成员名称?



正确 - 一个不是;构造如:


struct S {int a,b; };

struct T {int c,a; };


创建了问题,因为名为a的成员必须指两个

" offset 0"和偏移2。同时(sizeof(int)为2,

因为这个史前C只在PDP-11上被发现)。


实际上,你甚至可以写:


struct {char lo,hi; };

int var = someval;

...

var.lo =''a'';


生成的机器代码类似于:


*(char *)& var =''a'';


adb调试器使用了这个。


>但即使最初的原因与此相关,为什么在类型系统变强后保留
分区?



一些源代码 - 例如adb调试器,也许是

Bourne shell - 取决于此。截至20世纪90年代初,一些

程序仍然使用cc -W编译,其中-W选项

*抑制*警告。 (当然,这不是* gcc,其中-W

*添加*警告。)


(我帮助重写了adb,作为UCB努力的一部分,BSD网络版本通常可用,并删除各种机器依赖的



好​​的,好的。这些都是歇斯底里的原因......


但这些都没有理由,只是解释了为什么当前的情况。


因为继续造成这种混乱,没有理由......

如果我们放弃了 - >,将会发生什么?

你看到有什么问题吗?


jacob


jacob navia写道:


因为继续造成这种混乱,没有理由......

如果我们放弃 - >会怎么样?



许多现有代码会中断;没有人会继续使用

违规编译器,因为他们有更好的东西

与他们的时间相比,而不是任意特征的复制/>
关闭。


-

Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN


A quick search of this group and its FAQ, and elsewhere, have not
answered this question to my satisfaction. Apologies if I missed
something obvious, either in the literature or my reasoning.

Can someone tell me why "->" exists? The compiler knows the difference
between a structure and a pointer to a structure, so why can''t it just
let me write "foo.bar" in both cases and not have to go back and
rewrite things when I later decide I want a pointer instead of a value
or vice versa? I often change my mind about whether e.g. a function
should receive a pointer or the thing itself, and it''s mildly
inconvenient to have to change all the code that references the
members.

Is there a historical reason? I read that dmr article on the history
of C, and he mentions a time when "foo->bar" would work regardless of
the type of "foo", with "bar" just describing an offset and the type
of the member value, but this confuses me -- was one not allowed to
use the same member name in two different structure types? But even if
the original reason was related to this, why was the division kept
after the type system got stronger?

解决方案

In article <11**********************@d30g2000prg.googlegroups .com>
<no**********@gmail.comwrote:

>Can someone tell me why "->" exists?

History, as you surmise:

>The compiler knows the difference between a structure and a
pointer to a structure, so why can''t it just let me write
"foo.bar" in both cases [...] ?

There are some languages that do this, including some "debuggers
that speak C" (gdb in particular). C is just different, as usual. :-)

>I read that dmr article on the history
of C, and he mentions a time when "foo->bar" would work regardless of
the type of "foo", with "bar" just describing an offset and the type
of the member value, but this confuses me -- was one not allowed to
use the same member name in two different structure types?

Correct -- one was not; constructs like:

struct S { int a, b; };
struct T { int c, a; };

created problems, because the member named "a" had to mean both
"offset 0" and "offset 2" simultaneously (sizeof(int) being 2,
since this Prehistoric C was found only on the PDP-11).

In fact, you could even write:

struct { char lo, hi; };
int var = someval;
...
var.lo = ''a'';

which produced machine code similar to:

*(char *)&var = ''a'';

The "adb" debugger used this.

>But even if the original reason was related to this, why was the
division kept after the type system got stronger?

Some source code -- such as the "adb" debugger, and perhaps the
Bourne shell -- depended on this. As of the early 1990s, a few
programs were still compiled with "cc -W", where the -W option
*suppressed* warnings. (This was, of course, *not* gcc, where -W
*adds* warnings.)

(I helped rewrite "adb", as part of the effort at UCB to make
the "BSD networking release" generally available and remove
various machine dependencies.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Chris Torek wrote:

In article <11**********************@d30g2000prg.googlegroups .com>
<no**********@gmail.comwrote:

>Can someone tell me why "->" exists?


History, as you surmise:

>The compiler knows the difference between a structure and a
pointer to a structure, so why can''t it just let me write
"foo.bar" in both cases [...] ?


There are some languages that do this, including some "debuggers
that speak C" (gdb in particular). C is just different, as usual. :-)

>I read that dmr article on the history
of C, and he mentions a time when "foo->bar" would work regardless of
the type of "foo", with "bar" just describing an offset and the type
of the member value, but this confuses me -- was one not allowed to
use the same member name in two different structure types?


Correct -- one was not; constructs like:

struct S { int a, b; };
struct T { int c, a; };

created problems, because the member named "a" had to mean both
"offset 0" and "offset 2" simultaneously (sizeof(int) being 2,
since this Prehistoric C was found only on the PDP-11).

In fact, you could even write:

struct { char lo, hi; };
int var = someval;
...
var.lo = ''a'';

which produced machine code similar to:

*(char *)&var = ''a'';

The "adb" debugger used this.

>But even if the original reason was related to this, why was the
division kept after the type system got stronger?


Some source code -- such as the "adb" debugger, and perhaps the
Bourne shell -- depended on this. As of the early 1990s, a few
programs were still compiled with "cc -W", where the -W option
*suppressed* warnings. (This was, of course, *not* gcc, where -W
*adds* warnings.)

(I helped rewrite "adb", as part of the effort at UCB to make
the "BSD networking release" generally available and remove
various machine dependencies.)

OK, OK. Those are "hysterical reasons"...

But those are no reasons, just explanations of why the current
situation.

There are no reasons then, for continuing to make that confusion...
What would happen if we dropped "->"?

Do you see any real problems?

jacob


jacob navia wrote:

There are no reasons then, for continuing to make that confusion...
What would happen if we dropped "->"?

Lots of existing code would break; no-one would continue to use the
offending compilers, for the good reason that they have better things
to do with their time than copy with a feature arbitrarily being
switched off.

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


这篇关于“ - &gt;”的理由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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