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

Let's say I create a char array, and I assume that the char array is empty. If I check the value of the first element in the array ( arr[0] ), what would this expression be?

+6
source share
4 answers

It depends on where and how the array is declared.

If the array is declared in the file area (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 an array is declared in a block area (inside a function or block) and is not declared static and does not have an explicit initializer, then the contents of the array are indefinite (essentially garbage values, some of which may be traps).

If the array is explicitly initialized, it contains everything that was in the initializer.

EDIT

In response to the comments below, note that you should not rely on implicit initialization of range variables. If an array of blocks is needed to create a null array, use the initializer:

 char foo[N] = {0}; 

When the initializer has fewer elements than the array, the elements in the array corresponding to the elements of the initializer will be set to the specified value; all other entries will be implicitly initialized, as if they were declared static .

In the above example, this means that the first element foo explicitly set to 0 , and all other elements are implicitly set to 0 .

+16
source

If it is an auto variable, it will be filled with garbage unless you explicitly initialize it, so there is no default value. arr[0] is likely to contain an apparently random value until it is explicitly modified to contain something else.

Of course, if you initialized the array (this means that you filled the array with the initial values, explicitly using something like a memset() or for loop or a function call or some other way), you will get exactly what you expect: the value with which you initialized it.

Note that the difference is between declaration and initialization.

 void f(void) { int x; // (1) x = 10; // (2) } 

In (1), you declare an auto integer variable. It is undefined right now (trash). In (2) you initialize the variable. Now it has a value of 10 .

Of course, declaration and initialization can be done right away:

 void f(void) { int x = 10; } 

The same 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
source

Never expect a variable to have any particular value on the first initialization unless you specify it explicitly. It will be filled with random material if you do not install it yourself.

-1
source

upon array initialization. you allocate static memory. and then you get the values ​​of the allocated memory so that random values

if you want the whole array to be 0 (as per Hunter Macmillen’s remark)

 char arr[size] = { 0 } 

or use the memset() function

 memset(arr,0,sizeof_your_arr); 
-3
source

Source: https://habr.com/ru/post/927045/


All Articles