简体   繁体   中英

What is the default value of members of a char array in C?

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?

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 };
}

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.

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)

char arr[size] = { 0 }

or use memset() function

memset(arr,0,sizeof_your_arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM