When does the compiler auto initialize the arrays to it's default datatype?
I know that in this example the arrays will be initialized, except the fact that i was expecting (reading here) \0
value for char instead in my console there were spaces
(at least this is what i think because there is nothing).
#define MAX_LENGTH 25
// Array global
int vector[MAX_LENGTH];
char vector_char[MAX_LENGTH];
int main()
{
//Print int array
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << vector[i] << " "; // All outputs 0
}
cout << endl;
//Print char array and output text to see that there is
//Something printed
cout << "start";
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << vector_char[i] << " "; //All outputs...
}
cout <<"end"<< endl;
return 0;
}
Other two situations that I've encountered is when an array is internal to a function (where the values will be "garbage" or "impredictibile" E.g:
int main()
{
int internal_vector[MAX_LENGTH];
for(int i = 0;i < MAX_LENGTH; i++)
{
cout << internal_vector[i] << " ";
}
cout << endl;
return 0;
}
and another one when the array is a member of a class (if you try anything you will have the surprise that array will contain garbage values:
class MyClass
{
private:
int array[100];
...
}
So, when is an array automatically initialized to it's default datatype and when should i make my own function to initialize each element?