Pointers on C——8 Arrays.14

来源:互联网 发布:log4j2 java api 编辑:程序博客网 时间:2024/06/06 02:34

8.2.1 Storage Order

Consider this array:

考虑下面这个数组:


int array[3];


It contains three elements, as shown in this diagram.

它包含3 个元素,如下图所示:



But now suppose you are told that each of these elements is in fact an array of six elements? Here is the new declaration:

但现在假定你被告知这3 个元素中的每一个实际上都是包含6 个元素的数组,情况又将如何呢?下面是这个新的声明:


int array[3][6];


and here is how it looks:

下面是它在内存中的存储形式:



The solid boxes show the three elements of the first dimension, and the dotted divisions show the six elements of the second dimension. The subscripts of the elements above, from left to right, are:

实线方框表示第1 维的3 个元素,虚线用于划分第2 维的6 个元素。按照从左到右的顺序,上面每个元素的下标值分别是:




This example illustrates what is called the storage order of array elements. In C,elements of a multidimensional array are stored in the order given by varying their rightmost subscript most rapidly, called row major order. Knowing the storage order helps you answer some useful questions, for example, the order in which to write the values in an initializer list. What values does the following code print?

这个例子说明了数组元素的存储顺序(storage order) 。在C 中,多维数组的元素存储顺序按照最右边的下标率先变化的原则,称为行主序(row major order) 。知道了多维数组的存储顺序有助于回答一些有用的问题,比如你应该按照什么样的顺序来编写初始化列表的值。下面的代码段将会打印出什么样的值呢?


int matrix[6][10];

int *mp;

...

mp = &matrix[3][8];

printf( "First value is %d\n", *mp );

printf( "Second value is %d\n", *++mp );

printf( "Third value is %d\n", *++mp );


It is obvious that the first value printed will be the contents of matrix[3][8], but what is printed next? The storage order determines the answer—the next element is the one whose rightmost subscript has changed, namely matrix[3][9]. What about the next one? Column nine is the last column in the row. Because the rows of the matrix are stored one after another, the next element printed will be matrix[4][0].

很显然,第1 个被打印的值将是matrix[3][8]的内容,但下一个被打印的又是什么呢?存储顺序可以回答这个问题_——下一个元素将是最右边下标首先变化的那个,也就是matrix[3] [9]。再接下去又轮到谁呢?第9 列可是一行中的最后一列啦。不过,根据存储顺序规定,一行存满后就轮到下一行,所以下一个被打印的元素将是matrix[4][0]。


Here is a related question. Does matrix have three rows of ten columns each, or ten rows of three columns each? The answer may surprise you—in some contexts, it could be either way.

这里有一个相关的问题。matrix 到底是6 行10 列还是10 行6 列?答案可能会令你大吃一惊——在某些上下文环境中,两种答案都对。


Either way? How could it be two different things? Easy. If you use subscripts to put data into the array and you later use subscripts to look up values in the array,then it makes no difference whether you interpret the first subscript as the row number or the column number. As long as you do it the same way each time, it will work with either interpretation.

两种都对?怎么可能有两个不同的答案呢?这个简单,如果你根据下标把数据存放于数组中并在以后根据下标查找数组中的值,那么不管你把第1 个下标解释为行还是列,都不会有什么区别。只要你每次都坚持使用同一种方法,这两种解释方法都是可行的。


However, interpreting the first subscript as a row or as a column cannot change the storage order of the array. If you use the first subscript as the row number and the second as the column number, then accessing the elements one after another in their storage order will give you the data row by row. On the other hand, if you use the first subscript as the column number, then the same kind of access will give you your data in order column by column. You can choose whichever interpretation makes the most sense for your application. You cannot, however, change the way the elements are actually stored in memory. That order is defined by the Standard.

但是,把第1 个下标解释为行或列并不会改变数组的存储顺序。如果你把第1 个下标解释为行,把第2 个下标解释为列,那么当你按照存储顺序逐个访问数组元素时,你所获得的元素是按行排列的。另一方面,如果把第1 个下标作为列,那么当你按前面的顺序访问数组元素时,你所得到的元素是按列排列的。你可以在你的程序中选择更加合理的解释方法。但是,你不能修改内存中数组元素的实际存储方式。这个顺序是由标准定义的。


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

原创粉丝点击