Pointers on C——6 Pointers.4

来源:互联网 发布:洗衣软件 编辑:程序博客网 时间:2024/06/14 08:57

​6.4 Indirection Operator


The process of following a pointer to the location to which it points is called indirection or dereferencing the pointer. The operator that performs indirection is the unary *. Here are some examples using the declarations in the previous section.

通过一个指针访问它所指向的地址的过程称为间接访问(indirection)或解引用指针(dereferencing the pointer) 。这个用于执行间接访问的操作符是单目操作符*。这里有一些例子,它们使用了前面小节里的一些声明。



The value of d is 100. When we apply the indirection operator to d, it means to go to location 100 in memory and look there instead. Thus, the R‐value of *d is 112—the contents of location 100. The L‐value would be location 100 itself.

d 的值是100 。当我们对d 使用间接访问操作符时,它表示访问内存位置100 并察看那里的值。因此, *d 的右值是112——位置100 的内容,它的左值是位置100 本身。


    Note the types of the expressions in the list above: d is a pointer to an integer and dereferencing it produces an integer. Similarly, applying indirection to a float *produces a float.

注意上面列表中各个表达式的类型: d 是一个指向整型的指针,对它进行解引用操作将产生一个整型值。类似,对float *进行间接访问将产生一个float 型值。


Normally, we donʹt know which location the compiler will choose for each variable, so we cannot predict their addresses in advance. Thus, when drawing pictures of pointers in memory, it is inconvenient to use the actual numbers for addresses, so most books use arrows instead, like this:

正常情况下,我们并不知道编译器为每个变量所选择的存储位置,所以我们事先无法预测它们的地址。这样,当我们绘制内存中的指针图时,用实际数值表示地址是不方便的。所以绝大部分书籍改用箭头来代替,如下所示:


    However, this notation can be misleading because the arrows can trick you into doing indirection even when there is no indirection to be done. For example, what is the value of the expression d from the diagram above?

但是,这种记法可能会引起误解,因为箭头可以会使你误以为执行了间接访问操作,但事实上它并不一定会进行这个操作。例如,根据上图,你会推断表达式d 的值是什么?


If you answered 112, then you were tricked by the arrow. The correct answer is the address of a, not its contents. The arrow, though, seems to pull our eyes to a. It is hard not to follow the arrow and that is the problem: you must not follow the arrow unless there is an indirection operator.

如果你的答案是112 ,那么你就被这个箭头误导了。正确的答案是a 的地址,而不是它的内容。但是,这个箭头似乎会把你的注意力吸引到a 上。要使你的思维不受箭头影响是不容易的,这也是问题所在:除非存在间接引用操作符,否则不要被箭头所误导。


The modified arrow notation shown below tries to eliminate this problem.

下面这个修正后的箭头记法试图消除这个问题。


The intent is to show the value of the pointer without the strong visual cue that the arrow is a path that must be followed. Indeed, the value of a pointer variable is simply a collection of bits until an indirection is performed on it. When indirection is performed, a solid arrow is used to show what actually took place.

这种记法的意图是既显示指针的值,但又不给你强烈的视觉线索,以为这个箭头是我们必须遵从的路径。事实上,如果不对指针变量进行间接访问操作,它的值只是简单的一些位的集合。当执行间接访问操作时,这种记法才使用实线箭头表示实际发生的内存访问。


Note that the arrow originates inside of the box because it represents the value stored in that variable. Also, the arrow points to a location, not to the value in the location. This notation implies that following the arrow with indirection produces an L‐value. It does, as we shall see later.

注意箭头起始于方框内部,因为它表示存储于该变量的值。同样,箭头指向一个位置,而不是存储于该位置的值。这种记法提示跟随箭头执行间接访问操作的结果将是一个左值。事实也的确如此,我们在以后将看到这一点。


Although the arrow notation is useful, in order to be able to use it properly you must remember that the value of a pointer variable is just a number. The arrow shows the value of this number, but the arrow notation does not change the fact that it is just a number. A pointer has no built‐in property of indirection, so you must not follow the arrow unless there is art indirection operator that tells you to do so.

尽管这种箭头记法很有用,但为了正确地使用它,你必须记住指针变量的值就是一个数字。箭头显示了这个数字的值,但箭头记法并未改变它本身就是个数字的事实。指针并不存在内建的间接访问属性,所以除非表达式中存在间接访问操作符,否则你不能按箭头所示实际访问它所指向的位置。


上一章 Pointers on C——6 Pointers.3

原创粉丝点击