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?
For char arrays, the default value is '\0' . For an array of pointers, the default value is nullptr . For strings, the default value is an empty string "" . That's all about declaring and initializing arrays in C/C++.
In Java, the default value of char is “u0000”.
The elements of the array are initialized to the default value of the element type, 0 for integers.
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.
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
.
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.
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.
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
.
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 };
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With