With which values are initialized the elements of a char type array

2

I have the following program:

int main(){
    char condominio[20][20];


    return 0;
}

What values would each condominium element contain? Any special character? since when I print I do not see anything.

    
asked by Cristofer Fuentes 07.12.2016 в 01:11
source

4 answers

3
  

What values would each condominium element contain?

The content will be random or indeterminate, condominio will occupy 20 x 20 bytes (assuming that char occupies one byte) which will not be initialized.

That is to say: 400 bytes of memory will be used, whatever was in memory before being requested by condominio and that memory could have any value depending on how your operating system manages memory.

For example, there are operating systems that when they release memory do not erase the content of the same but they mark it as available, for example it could happen 1 that ...

char *frase = malloc(16);
memcpy(frase, "Mi mama me mima", 16);
free(frase);
char *otra_frase = malloc(16);

... the variable otra_frase would point to a memory containing "Mi mama me mima" even though that information would have been deleted with the free(frase) instruction.

Knowing this, the variable% co_of% without initializing its values could contain any memory residue from parallel or previous executions.

  

Any special character? since when I print I do not see anything.

I bet you printed like this:

for (int x = 0; x < 20; ++x)
{
    for (int y = 0; y < 20; ++y)
        printf("%c ", condominio[x][y]);
    printf("\n");
}

And you have seen absolutely nothing ... try to change print a character ( condominio ) by printing a number ( "%c " ):

for (int x = 0; x < 20; ++x)
{
    for (int y = 0; y < 20; ++y)
        printf("%d ", condominio[x][y]);
    printf("\n");
}

With that change, in my case the execution has shown 2 :

0 0 0 0 0 0 0 0 -64 -1 -97 -1 -118 127 0 0 48 -6 -97 -1 
-118 127 0 0 77 3 64 0 0 0 0 0 80 58 33 -1 -118 127 0 0 
-72 2 64 0 0 0 0 0 0 0 0 0 1 0 0 0 20 8 0 0 
1 0 0 0 0 0 0 0 0 0 0 0 32 38 -96 -1 -118 127 0 0 
-64 101 106 48 -2 127 0 0 46 78 61 -10 0 0 0 0 -64 -1 -97 -1 
-118 127 0 0 -32 101 106 48 -2 127 0 0 -56 34 -96 -1 -118 127 0 0 
-29 -124 126 -1 -118 127 0 0 0 0 0 0 0 0 0 0 -64 -1 -97 -1 
-118 127 0 0 1 0 0 0 -118 127 0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 -56 34 -96 -1 -118 127 0 0 0 0 0 0 
0 0 0 0 80 -11 -97 -1 -118 127 0 0 -104 102 106 48 -2 127 0 0 
-24 -75 55 -1 -118 127 0 0 0 -118 92 -1 -118 127 0 0 0 40 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 32 38 -96 -1 -118 127 0 0 
-32 -117 125 -1 1 0 0 0 0 0 0 0 0 0 0 0 80 -11 -97 -1 
-118 127 0 0 77 3 64 0 0 0 0 0 0 122 55 -1 -118 127 0 0 
64 50 42 -1 -118 127 0 0 -32 119 92 -1 -118 127 0 0 125 48 44 0 
0 0 0 0 6 0 0 0 0 0 0 0 -65 0 0 0 0 0 0 0 
-82 101 106 48 -2 127 0 0 -81 101 106 48 -2 127 0 0 -104 46 33 -1 
-118 127 0 0 33 4 64 0 0 0 0 0 1 0 0 0 0 0 0 0 
93 6 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 64 0 
0 0 0 0 0 0 0 0 0 0 0 0 112 4 64 0 0 0 0 0

The reason why nothing is printed are all those zeros that tell the standard output that the characters to be printed have finished (the zero ( "%d " ) indicates the end of the string). If coincidentally 0 would have had a printable character it would have been shown next to all subsequent printable characters until finding another zero.

If you change the code to print strings instead of characters:

for (int x = 0; x < 20; ++x)
    printf("%s\n", condominio[x]);

Every condiminio[0][0] that starts with a printable character will be printed; in my case I have seen the following nonsense:

4
�@

'd.P�a: 4
�]4
4


襦]4

�]
4
@"�]4

Nd.P�a: 4
�@

Note that using the value of uninitialized variables is undefined behavior, so anything unexpected could happen ... such as nothing being shown.

1 It might also not happen, it's dependent on how the operating system handles memory.

2 Content may vary with each execution.

    
answered by 09.12.2016 / 09:40
source
0
char condominio[20][20];

That line is creating a variable that is capable of storing 20 strings of characters, each of which can store up to 20 characters (counting the possible null end-of-string character).

What value does each chain contain? Its initial value is that which had the memory positions in which the variable is located. In C there are no constructors or initializers by default. If you want a memory location to have an initial value by default you will have to program it explicitly:

int array[10]; // Cada posición del array tendrá un valor aleatorio (basura)

int array[10] = {2}; // Cada valor del array se inicializa con valor 2

int array[10] = { 1,2,3 }; // Las tres primeras posiciones se incializan con la secuencia 1,2,3 y el resto con 0

int array[10] = { 1,2,3,4,5,6,7,8,9,10}; // Se le asigna un valor específico a cada elemento del array

static int array[10]; // Al ser un elemento estático se inicializa, por defecto, a 0.

Of course this type of initializations only serve for known values at compile time. For variable values you can iterate over the array with a loop, for example.

As for what you are saying about printing ... do not indicate how you are showing the variables on the screen, so that part remains pending for you to edit the question. Of course, keep in mind that not all values in the char range correspond to printable characters. Printing one of these characters can have the most varied results (line breaks, carriage returns, strange symbols, ...)

    
answered by 07.12.2016 в 01:56
0

Your question is based on a char type two-dimensional array, you can initialize it like this:

int main(){

    char *condominio[20][20] = {{"a", "b"},  {"c", "d"}};

    return 0;
}

To print your content you can do:

 int main(){

  char *condominio[20][20] ={{"a", "b"},  {"c", "d"}};

  for(int i = 0; i < 2; i++) 
  {
       for(int j = 0; j < 2; j++) 
       {
         printf("%s", condominio[i][j]);
       }
    printf("\n");
  } 
  return 0;

}

Obtaining as output something similar to:

ab
cd
    
answered by 07.12.2016 в 02:24
0

When an array is declared, if no value is specified when initializing it, all fields are equal to 0.

Therefore the 400 elements (20x20) of your matrix will have the value 0.

That's exactly why you do not see anything on the screen when you print it. 0 (zero) in ascii is equivalent to NULL. Printable characters range from 32 to 126 (in the reduced table)

    
answered by 07.12.2016 в 20:44