Pointers on C——8 Arrays.16

来源:互联网 发布:开淘宝网店策划书范文 编辑:程序博客网 时间:2024/05/19 14:15

​8.2.3 Subscripts

To identify a single element from a multidimensional array, one subscript must be given for each dimension, in the same order that the dimensions were given in the declaration, and each subscript is enclosed in its own set of brackets. With this declaration:

如果要标识一个多维数组的某个元素,必须按照与数组声明时相同的顺序为每一维都提供一个下标,而且每个下标都单独位于一对方括号内。在下面的声明中:


int matrix[3][10];

the expression

matrix[1][5]

accesses this element:


But subscripts are really indirection expressions in disguise, even with multidimensional arrays. Consider die expression matrix Its type is ʺpointer to an array of ten integersʺ, and its value is:

但是,下标引用实际上只是间接访问表达式的一种伪装形式,即使在多维数组中也是如此。考虑、下面这个表达式:



It points to the first array of ten integers.

它指向包含10 个整型元素的第l 个子数组。


The expression matrix+1 is also a ʺpointer to an array of ten integers,ʺ but it points to a different row of the matrix:

表达式matrix + 1也是一个"指向包含10 个整型元素的数组的指针",但它指向matrix 的另一行:



Why? Because the value one is scaled by the size of an array of ten integers. Applying indirection follows the arrow and selects this array:

为什么?因为1 这个值根据包含10 个整型元素的数组的长度进行调整,所以它指向matrix 的下一行。如果对其执行间接访问操作,就如下图随箭头选择中间这个子数组:



So the expression

*( matrix + 1 )


really identifies a particular array of ten integers. The value of an array name is a constant pointer to the first element in the array, and the same is true of this expression. Its type is ʺpointer to integer,ʺ and we can now show its value in the context of the next dimension:

事实上标识了一个包含10 个整型元素的子数组。数组名的值是个常量指针,它指向数组的第1个元素,在这个表达式中也是如此。它的类型是"指向整型的指针",我们现在可以在下一 维的上下文环境中显示它的值:



Now hold on to your hat. What is the result of this expression?

现在请拿稳你的帽子,猜猜下面这个表达式的结果是什么?


*( matrix + 1 ) + 5


The previous expression pointed to an integer, so value five is scaled by the size of an integer. The result points five integers after the original expression:

前一个表达式是个指向整型值的指针,所以5 这个值根据整型的长度进行调整。整个表达式的结果是一个指针,它指向的位置比原先那个表达式所指向的位置向后移动了5 个整型元素。



Applying indirection,

对其执行间接访问操作:


*( *( matrix + 1 ) + 5 )


takes us to the integer in question. If used as an R‐value, you would get the value stored there. As an L‐value, the location would receive a new value.

它所访问的正是图中的那个整型元素。如果它作为右值使用,你就取得存储于那个位置的值。如果它作为左值使用,这个位置将存储一个新值。


This intimidating expression is actually our old friend, the subscript. We can rewrite the subexpression *( matrix + 1 ) as matrix[1]. Substituting this subexpression into the original expression gives us:

这个看上去吓人的表达式实际主正是我们的老朋友——下标。我们可以把子表达式*(matrix + 1)改写为matrix[l] 。把这个下标表达式代入原先的表达式,我们将得到:

*( matrix[1] + 5 )


This expression is perfectly valid, matrix[1] selects an array of ten integers, so its type is a pointer to an integer. To this pointer we add five, and then apply indirection.

这个表达式是完全合法的。matrix[l] 选定一个子数组,所以它的类型是一个指向整型的指针。我们对这个指针加上5 ,然后执行间接访问操作。


But once again, we have the subscript form of indirection, so we can substitute and get

但是,我们可以再次用下标代替间接访问,所以这个表达式还可以写成:


matrix[1][5]


Thus, subscripts are simply another form of indirection expression even for multidimensional arrays.

这样,即使对于多维数组,下标仍然是另一种形式的间接访问表达式。


The point of this exercise is to illustrate how subscripts work for multidimensional arrays and how they depend on the notion of a pointer to an array.Subscripts are evaluated from left to right. The array name is a pointer to the first element of the first dimension, so the first subscript value is scaled by the size of that element. The result points to the desired element in that dimension. The indirection then selects that specific element. Because the element is an array, the type of this expression is a pointer to the first element of the next dimension. The next subscript is scaled by that size, and the process is repeated until all of the subscripts have been evaluated.

这个练习的要点在于它说明了多维数组中的下标引用是如何工作的,以及它们是如何依赖于指向数组的指针这个概念。下标是从左向右进行计算的,数组名是一个指向第1 维第1 个元素的指针,所以第1 个下标值根据该元素的长度进行调整。它的结果是一个指向那一维中所需元素的指针。间接访问操作随后选择那个特定的元素。由于该元素本身是个数组,所以这个表达式的类型是一个指向下一维第1 个元素的指针。下一个下标值根据这个长度进行调整,这个过程重复进行,直到所有的下标均计算完毕。


 In many other languages, multiple subscripts are written as a comma‐separated list of values. Some languages allow both forms. Not so in C: writing 

在许多其他语言中,多重下标被写作逗号分隔的值列表形式。有些语言这两种形式都允许,但C 并非如此:编写


matrix[4,3]


looks perfectly fine, but almost certainly does not accomplish what you want.Remember that the comma operator evaluates the first expression but throws away its value. The result is the value of the second expression. Thus, the previous expression is equivalent to

看上去没有问题,但它的功能和你想象的几乎肯定不同。记住,逗号操作符首先对第1 个表达式求值,但随即丢弃这个值。最后的结采是第2 个表达式的值。因此,前面这个表达式与下面这个表达式是相等的。


matrix[3]


The problem is that the expression may compile without any error or warning. The expression is perfectly legitimate, it just doesnʹt mean what you had intended.

问题在于这个表达式可以顺利通过编译,不会产生任何错误或警告信息。这个表达式是完全合法的,但它的意思跟你想象的根本不同。

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

原创粉丝点击