c char字符数组初始化 [英] C char array initialization
问题描述
我不知道会在以下几个方面初始化之后的char数组中的内容。
I'm not sure what will be in the char array after initialization in the following ways.
1 字符BUF [10] =;
结果
2. 字符BUF [10] =;
结果
3. 字符BUF [10] =A;
有关情况2,我觉得 BUF [0]
应,
BUF [1]
应'\\ 0'
,并从 BUF [2]
到 BUF [9]
将是随机的内容。对于情况3,我觉得 BUF [0]
应'A'
, BUF [ 1]
应该是'\\ 0',从 BUF [2]
到 BUF [9]
将是随机的内容。
For case 2, I think buf[0]
should be ' '
, buf[1]
should be '\0'
, and from buf[2]
to buf[9]
will be random content. For case 3, I think buf[0]
should be 'a'
, buf[1]
should be '\0', and from buf[2]
to buf[9]
will be random content.
这是否正确?
和为1的情况下,会在 BUF
是什么? BUF [0] =='\\ 0'
和 BUF [1]
到 BUF [9]
将是随机的内容?
And for the case 1, what will be in the buf
? buf[0] == '\0'
and from buf[1]
to buf[9]
will be random content?
推荐答案
这是不是你如何初始化数组,但对于:
This is not how you initialize an array, but for:
-
第一个声明:
The first declaration:
char buf[10] = "";
等同于
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
第二个声明:
The second declaration:
char buf[10] = " ";
等同于
char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0};
第三个声明:
The third declaration:
char buf[10] = "a";
等同于
char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0};
如你所见,没有随机的内容:如果有较少的初始化,数组的剩余与 0
初始化。这即使阵列函数内声明的情况。
As you can see, no random content: if there are fewer initializers, the remaining of the array is initialized with 0
. This the case even if the array is declared inside a function.
这篇关于c char字符数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!