Pointers on C——8 Arrays.2

来源:互联网 发布:指环王和哈利波特 知乎 编辑:程序博客网 时间:2024/05/23 13:47

8.1.2 Subscripts

In the context of the previous declarations, what is the meaning of this expression?

在前面声明的上下文环境中,下面这个表达式是什么意思?


*( b + 3 )


First, the value of b is a pointer to an integer, so the value three is scaled to the size of an integer. The addition yields a pointer to the integer that is located three integers beyond the first one in the array. The indirection then takes us to this new location,either to get the value there (R‐value) or to store a new one (L‐value).

首先, b 的值是一个指向整型的指针,所以3 这个值根据整型值的长度进行调整。加法运算的结果是另一个指向整型的指针,它所指向的是数组第1 个元素向后移3 个整数长度的位置。然后,间接访问操作访问这个新位置,或者取得那里的值(右值),或者把一个新值存储于该处(左值)。


If this process sounds familiar, it is because a subscript does exactly the same thing. We can now explain a statement that was mentioned in Chapter 5: except for its precedence, a subscript is exactly the same as an indirection. For example, the following expressions are equivalent:

这个过程昕上去是不是很熟悉?这是因为它和下标引用的执行过程完全相同。我们现在可以解释第5 章所提到的一句话:除了优先级之外,下标引用和间接访问完全相同。例如,下面这两个表达式是等同的:


array[subscript]

*( array + ( subscript ) )


Now that you know that the value of an array name is just a pointer constant, you can verify this equivalence. In the subscript expression, the subscript is evaluated first.Then the subscript value selects a specific array element. In the second expression, the inner parentheses guarantee that the subscript is evaluated first, as before. Using pointer arithmetic, the addition produces a pointer to the desired element. The indirection then follows that pointer, and voila—an array element.

既然你已知道数组名的值只是一个指针常量,你可以证明它们的相等性。在那个下标表达式中,子表达式subscript首先进行求值。然后,这个下标值在数组中选择一个特定的元素。在第2 个表达式中,内层的那个括号保证子表达式subscript 像前一个表达式那样首先进行求值。经过指针运算,加法运算的结果是一个指向所需元素的指针。然后,对这个指针执行间接访问操作,访问它指向的那个数组元素。


Wherever a subscript is used, the equivalent pointer expression can also be used; and wherever a pointer expression of the form shown above is used, you can also use a subscript.

在使用下标引用的地方,你可以使用对等的指针表达式来代替。在使用上面这种形式的指针表达式的地方,你也可以使用下标表达式来代替。


Here is a little exercises to illustrate this equivalence.

这里有个小例子,可以说明这种相等性。


int array[10];

int *ap = array + 2;


Remember the scaling that is performed on the addition: the result is that ap points at

记住,在进行指针加法运算时会对2 进行调整。运算结果所产生的指针ap 指向array[2] ,如下所示:


array[2], like this:

For each of the following expressions involving ap, see if you can think of an equivalent expression that uses array.

在下面各个涉及ap 的表达式中,看看你能不能写出使用array 的对等表达式。


ap This one is easy, you can just read the answer from the initialization:array + 2. The expression &array[2] is equivalent.

这个很容易,你只要阅读它的初始化表达式就能得到答案: array+2 。另外, &array[2]也是与它对等的表达式。


*ap Another easy one: the indirection follows the pointer to the location it is pointing at, which is array[2]. You could also write *(array + 2).

这个也很容易,间接访问跟随指针访问它所指向的位置,也就是array[2] 。你也可以这样写: * (array+2) 。


ap[0] ʺYou canʹt do that, ap isnʹt an array!ʺ If you thought something like this, youʹre stuck in the ʺcanʹt do it in other languagesʺ rut. Remember,a subscript in C is exactly like an indirection expression, so you can use one anywhere you can use indirection. In this case, the equivalent expression is *( ap + ( 0 ) ) which, after getting rid of the zero and the parentheses, ends up being identical to the previous expression.Therefore it has the same answer: array [2].

"你不能这样做, ap 不是一个数组!"如果你是这样想的,你就陷入了"其他语言不能这样做"这个惯性思维中了。记住, C 的下标引用和间接访问表达式是一样的。在现在这种情况下,对等的表达式是*(ap+(0)) ,除去0 和括号,其结果与前一个表达式相等。因此,它的答案和上一题相同: array[2] 。


ap + 6 If ap points to array[2], this addition gives a pointer to the location six integers later in the array, which is equivalent to array + 8 or &array[8].

如果ap 指向array[2] ,这个加法运算产生的指针所指向的元素是array[2] 向后移动6个整数位置的元素。与它对等的表达式是array+8 或&array[8] 。


上一章 Pointers on C——8 Arrays.1


原创粉丝点击