天天看點

Pointers on C——6 Pointers.4

​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) 。這個用于執行間接通路的操作符是單目操作符*。這裡有一些例子,它們使用了前面小節裡的一些聲明。

Pointers on C——6 Pointers.4

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:

正常情況下,我們并不知道編譯器為每個變量所選擇的存儲位置,是以我們事先無法預測它們的位址。這樣,當我們繪制記憶體中的指針圖時,用實際數值表示位址是不友善的。是以絕大部分書籍改用箭頭來代替,如下所示:

Pointers on C——6 Pointers.4

    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.

下面這個修正後的箭頭記法試圖消除這個問題。

Pointers on C——6 Pointers.4

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