Say I create a char
array, and I assume the char array is empty. If I check the value of the first element in the array (arr[0]
), what would be the result of this expression?
假设我创建了一个char数组,并假设char数组为空。如果我检查数组中第一个元素的值(arr [0]),这个表达式的结果是什么?
4 个解决方案
#1
16
It depends on where and how the array is declared.
它取决于声明数组的位置和方式。
If the array is declared at file scope (outside of any function), or is declared static
, and does not have an explicit initializer, then the contents of the array will be initialized to 0.
如果数组在文件范围内声明(在任何函数之外),或者声明为静态,并且没有显式初始值设定项,则数组的内容将初始化为0。
If the array is declared at block scope (within a function or block) and is not declared static
, and does not have an explicit initializer, then the contents of the array are indeterminate (essentially, garbage values, some of which may be trap representations).
如果数组是在块作用域(在函数或块内)声明并且未声明为静态,并且没有显式初始化器,则数组的内容是不确定的(实际上,垃圾值,其中一些可能是陷阱表示) )。
If the array has been explicitly initialized, then it contains whatever was in the initializer.
如果数组已经显式初始化,那么它包含初始化程序中的任何内容。
EDIT
编辑
In response to the comments below, note that you shouldn't rely on implicit initialization for block-scope variables. If you need a block-scope array to be zeroed out on creation, use an initializer:
在回应下面的注释时,请注意您不应该依赖于块范围变量的隐式初始化。如果需要在创建时将块范围数组清零,请使用初始化程序:
char foo[N] = {0};
When there are fewer elements in the initializer than there are in the array, elements in the array corresponding to elements in the initializer will be set to the value specified; all remaining entries will be implicitly initialized as if they were declared static
.
当初始化器中的元素少于数组中的元素时,数组中与初始化程序中的元素对应的元素将被设置为指定的值;所有剩余的条目将被隐式初始化,就像它们被声明为静态一样。
In the example above, this means that the first element of foo
is explicitly set to 0
, while all the remaining elements are implicitly set to 0
.
在上面的示例中,这意味着foo的第一个元素显式设置为0,而所有剩余的元素都隐式设置为0。
#2
3
If it's an auto
variable, it will be filled with junk unless you explicitly initialize it, so there is no default value. arr[0]
will likely contain a seemingly random value until explicitly changed to contain something else.
如果它是一个自动变量,它将填充垃圾,除非你明确初始化它,所以没有默认值。 arr [0]可能包含一个看似随机的值,直到明确更改为包含其他内容。
Of course, if you initialized the array (meaning that you filled the array with initial values explicitly using something like memset()
or a for
loop or a function call or whatever other means), you'll get exactly what you expect: the value with which you initialized it.
当然,如果您初始化数组(意味着您使用类似memset()或for循环或函数调用或其他任何方法显式填充数组的初始值),您将获得您期望的值:值用它来初始化它。
Do note though the difference between declaration and initialization.
请注意声明和初始化之间的区别。
void f(void) {
int x; // (1)
x = 10; // (2)
}
At (1), you're declaring an auto
integer variable. It has an undefined value right now (junk). At (2), you're initializing the variable. It now has the value of 10
.
在(1)处,您声明了一个自动整数变量。它现在有一个未定义的值(垃圾)。在(2),您正在初始化变量。它现在的值为10。
Of course, declaration and initialization can both be done at once:
当然,声明和初始化都可以一次完成:
void f(void) {
int x = 10;
}
The same thing is true for arrays:
数组也是如此:
void f(void) {
int x[2]; // x contains 2 junk values, so x[0] == junk
x[0] = 1; // x contains { 1, junk }, so x[0] == 1
x[1] = 2; // x contains { 1, 2 }, so x[0] == 1
}
or, to declare and initialize it:
或者,声明并初始化它:
void f(void) {
int x[2] = { 1, 2 };
}
#3
-1
Never expect any variable to have any certain value when you first initialize it unless you specify it explicitly. It will be filled with random stuff unless you set it yourself.
除非明确指定,否则永远不要指望任何变量在初始化时具有任何特定值。除非你自己设置,否则它将随机填充。
#4
-3
when initiate array. your are allocating a static memory. and then you will get the values of allocated memory so it's random values
何时启动数组。你正在分配一个静态内存。然后你将获得已分配内存的值,因此它是随机值
if you want set the whole array to 0 then (According to Hunter McMillen Remark)
如果你想将整个数组设置为0那么(根据Hunter McMillen Remark)
char arr[size] = { 0 }
or use memset()
function
或使用memset()函数
memset(arr,0,sizeof_your_arr);
#1
16
It depends on where and how the array is declared.
它取决于声明数组的位置和方式。
If the array is declared at file scope (outside of any function), or is declared static
, and does not have an explicit initializer, then the contents of the array will be initialized to 0.
如果数组在文件范围内声明(在任何函数之外),或者声明为静态,并且没有显式初始值设定项,则数组的内容将初始化为0。
If the array is declared at block scope (within a function or block) and is not declared static
, and does not have an explicit initializer, then the contents of the array are indeterminate (essentially, garbage values, some of which may be trap representations).
如果数组是在块作用域(在函数或块内)声明并且未声明为静态,并且没有显式初始化器,则数组的内容是不确定的(实际上,垃圾值,其中一些可能是陷阱表示) )。
If the array has been explicitly initialized, then it contains whatever was in the initializer.
如果数组已经显式初始化,那么它包含初始化程序中的任何内容。
EDIT
编辑
In response to the comments below, note that you shouldn't rely on implicit initialization for block-scope variables. If you need a block-scope array to be zeroed out on creation, use an initializer:
在回应下面的注释时,请注意您不应该依赖于块范围变量的隐式初始化。如果需要在创建时将块范围数组清零,请使用初始化程序:
char foo[N] = {0};
When there are fewer elements in the initializer than there are in the array, elements in the array corresponding to elements in the initializer will be set to the value specified; all remaining entries will be implicitly initialized as if they were declared static
.
当初始化器中的元素少于数组中的元素时,数组中与初始化程序中的元素对应的元素将被设置为指定的值;所有剩余的条目将被隐式初始化,就像它们被声明为静态一样。
In the example above, this means that the first element of foo
is explicitly set to 0
, while all the remaining elements are implicitly set to 0
.
在上面的示例中,这意味着foo的第一个元素显式设置为0,而所有剩余的元素都隐式设置为0。
#2
3
If it's an auto
variable, it will be filled with junk unless you explicitly initialize it, so there is no default value. arr[0]
will likely contain a seemingly random value until explicitly changed to contain something else.
如果它是一个自动变量,它将填充垃圾,除非你明确初始化它,所以没有默认值。 arr [0]可能包含一个看似随机的值,直到明确更改为包含其他内容。
Of course, if you initialized the array (meaning that you filled the array with initial values explicitly using something like memset()
or a for
loop or a function call or whatever other means), you'll get exactly what you expect: the value with which you initialized it.
当然,如果您初始化数组(意味着您使用类似memset()或for循环或函数调用或其他任何方法显式填充数组的初始值),您将获得您期望的值:值用它来初始化它。
Do note though the difference between declaration and initialization.
请注意声明和初始化之间的区别。
void f(void) {
int x; // (1)
x = 10; // (2)
}
At (1), you're declaring an auto
integer variable. It has an undefined value right now (junk). At (2), you're initializing the variable. It now has the value of 10
.
在(1)处,您声明了一个自动整数变量。它现在有一个未定义的值(垃圾)。在(2),您正在初始化变量。它现在的值为10。
Of course, declaration and initialization can both be done at once:
当然,声明和初始化都可以一次完成:
void f(void) {
int x = 10;
}
The same thing is true for arrays:
数组也是如此:
void f(void) {
int x[2]; // x contains 2 junk values, so x[0] == junk
x[0] = 1; // x contains { 1, junk }, so x[0] == 1
x[1] = 2; // x contains { 1, 2 }, so x[0] == 1
}
or, to declare and initialize it:
或者,声明并初始化它:
void f(void) {
int x[2] = { 1, 2 };
}
#3
-1
Never expect any variable to have any certain value when you first initialize it unless you specify it explicitly. It will be filled with random stuff unless you set it yourself.
除非明确指定,否则永远不要指望任何变量在初始化时具有任何特定值。除非你自己设置,否则它将随机填充。
#4
-3
when initiate array. your are allocating a static memory. and then you will get the values of allocated memory so it's random values
何时启动数组。你正在分配一个静态内存。然后你将获得已分配内存的值,因此它是随机值
if you want set the whole array to 0 then (According to Hunter McMillen Remark)
如果你想将整个数组设置为0那么(根据Hunter McMillen Remark)
char arr[size] = { 0 }
or use memset()
function
或使用memset()函数
memset(arr,0,sizeof_your_arr);