Hi all, my question is that
int a[2]={0};
cout<<a[1];
output is o
but if i do
int a[2];
a[0]=1;
cout<<a[2];
output is a garbage value on g++ ubuntu 10.04
in both case’s it’s partial initialization of array’s then why different output’s???
Hi all, my question is that
int a[2]={0};
cout<<a[1];
output is o
but if i do
int a[2];
a[0]=1;
cout<<a[2];
output is a garbage value on g++ ubuntu 10.04
in both case’s it’s partial initialization of array’s then why different output’s???
When you initialize
int a[SIZE] = {0};
The compiler initializes all the elements to zero, not just one.
And when you specifically initialize a[0]
, then obviously a[2]
was not initialized yet, and hence garbage value.
P.S. This works only when you initialize with 0
.
If you write
int a[3] = {1};
Then only a[0] will be initialized to 1, and rest zero.
Experiment more with initializations, and make yourself clear! Ask doubts, if you fumble again.
only the array elements gets initialised and the other elements are set to NULL or zero
for eg.: int a[10]={1,2,3,4,5}
where a[0] to a[4] gets initialised to values and from a[5] to a[9] are set as zero or NULL