Pointers on C——8 Arrays.19

​8.2.6 Initialization


The storage order of array elements becomes important when initializing multidimensional arrays. There are two ways to write the initializer list. The first is to just give a long list of initializers, as in the following example.

在初始化多维数组时,数组元素的存储顺序就变得非常重要。编写初始化列表有两种形式。第1 种是只给出一个长长的初始值列表,如下面的例子所示。

int matrix[2][3] = { 100, 101, 102, 110, 111, 112 };


The storage order has the rightmost subscript varying most rapidly, so this initialization produces the same result as these assignments:

多维数组的存储顺序是根据最右边的下标率先变化的原则确定的,所以这条初始化语句和下面这些赋值语旬的结果是一样的:


matrix[0][0] = 100;

matrix[0][1] = 101;

matrix[0][2] = 102;

matrix[1][0] = 110;

matrix[1][1] = 111;

matrix[1][2] = 112;


The second way is based on the idea that a multidimensional array is really a onedimensional array of complicated elements. For example, hereʹs a declaration for a two‐dimensional array:

第2 种方法基于多维数组实际上是复杂元素的一维数组这个概念。例如,下面是一个二维数组的声明:


int two_dim[3][5];


View two_dim as an array of three (complicated) elements. To initialize an array of three elements, we use a list containing three initializers:

我们可以把tow dim 看成是一个包含3 个(复杂的)元素的一维数组。为了初始化这个包含3个元素的数组,我们使用一个包含3 个初始内容的初始化列表:


int two_dim[3][5] = { ▲, ▲, ▲ };


But each of these elements is actually an array of five integers, so each of the ʹs should be a list of five values enclosed in braces. Replacing each  with such a list results in code that looks like this:

但是,该数组的每个元素实际上都是包含5 个元素的整型数组,所以每个的初始化列表都应该是一个由一对花括号包围的5 个整型值。用这类列表替换每个将产生如下代码:


int two_dim[3][5] = {

{ 00, 01, 02, 03, 04 },

{ 10, 11, 12, 13, 14 },

{ 20, 21, 22, 23, 24 }

};


Of course, the indenting and spacing we have used are not required, although they do make the list easier to read.

当然,我们所使用的缩进和空格并非必需,但它们使这个列表更容易阅读。


If you erase all but the outermost braces from this example, you will see that what is left is a simple initializer list like the first one. The braces merely delimit the initializer list by row.

如果你把这个例子中除了最外层之外的花括号都去掉,剩下的就是和第1 个例子一样的简单初始化列表。那些花括号只是起到了在初始化列表内部逐行定界的作用。


Figures 8.1 and 8.2 illustrate initializing three‐ and four‐dimensional arrays. In these examples, the digits of each initializer show the subscript values of the location in which it is stored.

图8.1 和图8.2 显示了三维和四维数组的初始化。在这些例子中,每个作为初始值的数字显示了它的存储位置的下标值。


default int three_dim[2][3][5] = {

{

{ 000, 001, 002, 003, 004 },

{ 010, O11, 012, 013, 014 },

{ 020, 021, 022, 023, 024 }

},

{ 100, 101, 102, 103, 104 },

{ 110, 111, 112, 113, 114 },

{ 120, 121, 122, 123, 124 }

}

};

Figure 8.1 Initializing a three‐dimensional array values. Here is how we can accomplish this task:


Why go to all the trouble of putting in these braces when the initialization works exactly the same way without them? There are two reasons. The first is organization.A single, long list of numbers makes it difficult to see which value belongs to each location of the array, thus the braces act as signposts and make it easier to verify that the right values appear in the right places.

既然加不加那些花括号对初始化过程不会产生影响,那么为什么要不反其烦地加上它们呢?这里有两个原因。首先是它有利于显示数组的结构。一个长长的单一数字列表使你很难看清哪个值位于数组中的哪个位址。因此,花括号起到了路标的作用,使你更容易确信正确的值出现在正确的位值。


Second, the braces are valuable for incomplete initialization. Without them,only trailing initializers may be omitted. Even if only a few elements of a large multidimensional array were to be initialized, the list might still be quite long because only the trailing initializers can be omitted. With the braces, though, trailing initializers may be omitted from each list. Also, the initializers for each dimension also form a list.

其次,对于不完整的初始化列表,花括号就相当有用。如果没有这些花括号,你只能在初始化列表中省略最后几个初始值。即使一个大型多维数组只有几个元素需要初始化,你也必须提供一个非常长的初始化列表,因为中问元素的初始值不能省略。但是,如果使用了这些花括号,每个子初始列表都可以省咯尾部的几个初始值。同时,每一维的初始列表各自都是一个初始化列表。


To illustrate this point, letʹs revisit the four‐dimensional array initialization in Figure 8.2 and change our requirements a little. Suppose we only need to initialize two elements in the array. Element [0][0][0][0] should be 100 and element [1][0][0][0] should be 200, All other elements should be left to their  values. Here is how we can accomplish this task:

为了说明这个概念,让我们重新观察图8.2 的四维数组初始化列表,并略微改变一下我们的要求。假定我们只需要对数组的两个元素进行初始化,元素[0] [0] [0] [0] 初始化为100 ,元素[1][0][0][0]初始化为200 ,其余的元素都缺省地初始化为0 。下面是我们用于完成这个任务的方法:



Without the braces, you would need this longer initializer list:

如果初始化列表内部不使用花括号,我们就需要下面这个长长的初始化列表:


int four_dim[2][2][3][5] = { 100, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200 };


This list is not only harder to read but also more difficult to get right in first place.

这个列表不仅难于阅读,而且一开始要准确地把100 和200 这两个值放到正确的位置都很困难。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值