符号X的含义是X所代表的地址?—— 怎么理解《C专家编程》中文版第83页的图4-1

符号X的含义是X所代表的地址?—— 如何理解《C专家编程》中文版第83页的图4-1?
如何理解《C专家编程》中文版第83页的图4-1?

在《C专家编程》中文版(人民邮电出版社2002年第一版)的第83页图4-1中,有这样的文字:

“X = Y;
在这个上下文环境里,符号X的含义是X所代表的地址。
在这个上下文环境里,符号Y的含义是Y所代表的地址的内容。”

乍一看,我们可能会非常困惑:一个变量名怎么同时又是它所代表的地址呢?难道X与&X恒等?

有网友已经就这个问题,提出诘责。

那么,我们不妨回到作者的原文中,仔细看看作者想说的,到底是个啥意思……

作者所撰写的原文是这样的:

In this section we show the difference between a reference using an array and a reference using a pointer. The first distinction we must note is between address y and contents of address y. This is actually quite a subtle point, because in most programming languages we use the same symbol to represent both, and the compiler figures out which is meant from the context. Take a simple assignment, as shown in Figure 4-1.

(Figure 4-1 即 图4-1,此处略。)

The symbol appearing on the left of an assignment is sometimes called an l-value (for "left-hand-side" or "locator" value), while a symbol on the right of an assignment is sometimes called an r-value (for "right-hand-side"). The compiler allocates an address (or l-value) to each variable. This address is known at compiletime, and is where the variable will be kept at runtime. In contrast, the value stored in a variable at runtime (its r-value) is not known until runtime. If the value stored in a variable is required, the compiler emits code to read the value from the given address and put it in a register.
 

The key point here is that the address of each symbol is known at compiletime. So if the compiler needs to do something with an address (add an offset to it, perhaps), it can do that directly and does not need to plant code to retrieve the address first. In contrast, the current value of a pointer must be retrieved at runtime before it can be dereferenced (made part of a further look-up). 

(摘录结束)

在上面所摘录的原文里面,有一些话值得注意。它们显明了作者举出Figure 4-1 例子的意图。

(i)The first distinction we must note is between address y and contents of address y. This is actually quite a subtle point, because in most programming languages we use the same symbol to represent both, and the compiler figures out which is meant from the context.

—— 作者强调必须首先厘清一个认识,即:地址y和地址y的内容(我不知道在简体中文译本中,是否将它们翻译成了“y的地址”和“y的地址上的内容”或类似的文字。我在此索性就直译。)是迥然不同的。作者认为这种区别是“狡猾”或“微妙”的(有不易察觉的意味),理由是,在绝大多数编程语言的使用境况中,编程者在代码中使用相同的符号(肉眼看起来一模一样的东东)来指代它们两者,而另一方面,作为编程者的“死对头”(终极老Boss)——编译器,则要根据所谓代码的“上下文”,来重新分辨它们究竟分别属于上述两种情况中的哪一种。

在这里,作者提到了“上下文(the context)”,这个概念具体化一些说,其实就是,以等号(the assignment)为分野标志的,符号之间的前后位置关系的一种体现。

请注意,在 Figure 4-1的示意图中,作者也特别写了“in this context”这个限定性状语。

(ii)The compiler allocates an address (or l-value) to each variable. This address is known at compiletime, and is where the variable will be kept at runtime. In contrast, the value stored in a variable at runtime (its r-value) is not known until runtime.

—— 作者在这句话之前,已经从形式上介绍了什么是 l-value什么是 r-value。接着,作者在这句话中,阐述了编译器处理这两种东东,分别采用的举措是什么:对于l-value,老Boss是为其分配地址。这个地址显然是老Boss在编译时就“心中有数”的(即编译时已知)。并且,跟这些地址相关的东东,到将来运行的时候,仍然保持恒定。而对于r-value,则 “与 l-value情况相反”,它所要承载的数据值,直到运行的时候,才能被确定。

请注意,这句话所在的段落,是紧跟在Figure 4-1下面的,其实就是对Figure 4-1里面的图像和文字的解释。所以,如果觉得Figure 4-1里的东东有什么问题,应该在这里寻找到详细的解答。

(iii)在第三段文字中,作者阐释了老Boss对于在编译时就为它所掌控的地址的处理方案。

综上所述,作者围绕 Figure 4-1的整篇文字,都是在讲述老Boss是怎么怎么行事的。Figure 4-1里的“in this context”,是老Boss眼中的“context”,而不是编程者在编写代码时肉眼所分辨的“context”。

这个道理,再讲得糙一点儿,就是:

难道要作者像下面这样讲?

  “ X = Y; X 代表了 X 这个变量; Y代表了Y这个变量(的内容)。”

那这不就退化成谭老教授的教材了嘛……

呵呵……

以上,仅供参考。







------解决方案--------------------
国内的书就对付看吧