如果我将一个 char 数组初始化为零/{0},我是否必须以 null 终止?
if I initialize a char array to zero/{0} do I have to null terminate?
例如,我声明了一个 char 数组,其中所有值都设置为零,方式如下:
char array[4] = {0};
如果我给它赋值,例如:
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
我需要像这样用 null 终止数组吗?:
array[3] = '[=12=]';
或者操作[=13=]是否在任何赋值之前终止?
这个声明
char array[4] = {0};
等同于
char array[4] = { 0, 0, 0, 0 };
来自C标准(6.7.9初始化)
19 The initialization shall occur in initializer list order, each
initializer provided for a particular subobject overriding any
previously listed initializer for the same subobject;151) all
subobjects that are not initialized explicitly shall be initialized
implicitly the same as objects that have static storage duration.
和
10 If an object that has automatic storage duration is not initialized
explicitly, its value is indeterminate. If an object that has static
or thread storage duration is not initialized explicitly, then:
— if it has arithmetic type, it is initialized to (positive or
unsigned) zero;
— if it is an aggregate, every member is initialized (recursively)
according to these rules, and any padding is initialized to zero bits;
...
所以元素数组[3]包含0。直到它被覆盖,数组包含一个字符串。
另一种由字符数组的零初始化的方法如下
char array[4] = "";
或以下
char array[4] = { "" };
甚至以下
char array[] = { [3] = '[=14=]' };
这是一个演示程序。
#include <stdio.h>
int main(void)
{
char array[] = { [3] = '[=15=]' };
array[0] = 'A'; // the array contains a string
printf( "%s\n", array );
array[1] = 'B'; // the array contains a string
printf( "%s\n", array );
array[2] = 'C'; // the array contains a string
printf( "%s\n", array );
array[3] = 'D'; // the array does not contain a string
printf( "%.*s\n", ( int )sizeof( array ), array );
return 0;
}
程序输出为
A
AB
ABC
ABCD
Do I need to null terminate the array like so?
没有。 '[=10=]'
只是 0
的另一种写法。这是一种编写 self-documenting 代码的方法,专门引用 null 终止符,这超出了传统。 (它实际上只是一个写成八进制转义序列的零。)
由于您已经将所有项目设置为 0
,因此不需要额外的 [=13=]
。
例如,我声明了一个 char 数组,其中所有值都设置为零,方式如下:
char array[4] = {0};
如果我给它赋值,例如:
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
我需要像这样用 null 终止数组吗?:
array[3] = '[=12=]';
或者操作[=13=]是否在任何赋值之前终止?
这个声明
char array[4] = {0};
等同于
char array[4] = { 0, 0, 0, 0 };
来自C标准(6.7.9初始化)
19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
和
10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
— if it has arithmetic type, it is initialized to (positive or unsigned) zero; — if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits; ...
所以元素数组[3]包含0。直到它被覆盖,数组包含一个字符串。
另一种由字符数组的零初始化的方法如下
char array[4] = "";
或以下
char array[4] = { "" };
甚至以下
char array[] = { [3] = '[=14=]' };
这是一个演示程序。
#include <stdio.h>
int main(void)
{
char array[] = { [3] = '[=15=]' };
array[0] = 'A'; // the array contains a string
printf( "%s\n", array );
array[1] = 'B'; // the array contains a string
printf( "%s\n", array );
array[2] = 'C'; // the array contains a string
printf( "%s\n", array );
array[3] = 'D'; // the array does not contain a string
printf( "%.*s\n", ( int )sizeof( array ), array );
return 0;
}
程序输出为
A
AB
ABC
ABCD
Do I need to null terminate the array like so?
没有。 '[=10=]'
只是 0
的另一种写法。这是一种编写 self-documenting 代码的方法,专门引用 null 终止符,这超出了传统。 (它实际上只是一个写成八进制转义序列的零。)
由于您已经将所有项目设置为 0
,因此不需要额外的 [=13=]
。