Pointers on C——8 Arrays.12

8.1.9 Incomplete Initialization


What happens with these two declarations?

在下面两个声明中会发生什么情况呢?


int vector[5] = { 1, 2, 3, 4, 5, 6 };

int vector[5] = { 1, 2, 3, 4 };


In both cases, the number of initializers does not match the size of the array. The first declaration is an error; there is no way to pack six integer values into five integer variables. The second, however, is legal. It provides values for the first four elements of the array; the last element is initialized to zero.

在这两种情况下,初始化值的数目和数组元素的数目并不匹配。第1 个声明是错误的,我们没有办法把6 个整型值装到5 个整型变量中。但是,第2 个声明却是合法的,它为数组的前4 个元素提供了初始值,最后一个元素则初始化为0 。


Can the middle values be omitted from the list as well?

那么,我们可不可以省略列表中间的那些值呢?


int vector[5] = { 1, 5 };


The compiler can only tell that there are not enough initializers, but it has no way to figure out which ones are missing. Therefore, only trailing initializers can be omitted.

编译器只知道初始值不够,但它无法知道缺少的是哪些值。所以,只允许省略最后几个初始值。


8.1.10 Automatic Array Sizing

Here is an example of another useful technique.

这里是另一个有用技巧的例子。


int vector[] = { 1, 2, 3, 4, 5 };


If the array size is missing, the compiler makes the array just big enough to hold the initializers that were given. This technique is particularly helpful if the initializer list is frequently modified.

如果声明中并未给出数组的长度,编译器就把数组的长度设置为刚好能够容纳所有的初始值的长度。如果初始值列表经常修改,这个技巧尤其有用。


8.1.11 Character Array Initialization

From what we have said so far, you might think that character arrays are initialized like this:

根据目前我们所学到的知识,你可能认为字符数组将以下面这种形式进行初始化:


char message[] = { 'H', 'e', 'l', 'l', 'o', 0 };


The code works, but it is cumbersome for all but the shortest strings. Therefore, the language Standard provides a shorthand notation for initializing character arrays:

这个方法当然可行。但除了非常短的字符串,这种方法确实很笨拙。因此,语言标准提供了一种快速方法用于初始化字符数组:


char message[] = "Hello";


Although this looks like a string literal, it is not. It is simply an alternate way of writing the initializer list in the previous example.

尽管它看上去像是一个字符串常量,实际上并不是。它只是前例的初始化列表的另一种写法。


How can you tell the difference between string literals and these shorthand initializer lists if they look exactly the same? They are distinguished from one another by the context in which theyʹre used. When initializing a character array, it is an initializer list. Everywhere else it is a string literal.

如果它们看上去完全相同,你如何分辨字符串常量和这种初始化列表快速记法呢?它们是根据它们所处的上下文环境进行区分的。当用于初始化一个字符数组时,它就是一个初始化列表。在其他任何地方,它都表示一个字符串常量。


Here is an example:

这里有二个例子:


char message1[] = "Hello";

char *message2 = "Hello";


The initializers look alike, but they have different meanings. The first initializes the elements of a character array, but the second is a true siring literal. The pointer variable is initialized to point to wherever the literal is stored, as illustrated below:

这两个初始化看上去很像,但它们具有不同的含义。前者初始化一个字符数组的元素,而后者则是一个真正的字符串常量。这个指针变量被初始化为指向这个字符串常量的存储位置,如下图所示:



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值