initialisation

why declaring statements like this- int a[100]={1}; sometimes work properly sometimes do not work properly ending to a great frustation??

If you are doing,

int a[size]={v};

this statement sets arr[0] to v i.e. the first element to the values specified inside the braces. If you are setting the value for atleast one element, the whole array becomes initialised and thus unspecified elements gets initialised implicitly to 0.

1 Like

Whenever you write the statement :

int arr[100]={k};

What this does is this:

arr[0]=k
arr[1]=0
arr[2]=0
arr[3]=0
…
arr[100]={0}

So Whenever you have to initialize your array to “0”, you can do:

int arr[100]={0}; 

it will work fine. But if you need to initialize your array to some other number always use a loop.
I hope this clears your doubt.

1 Like

I didn’t know also - it’s described in “Initializing arrays” section - http://www.cplusplus.com/doc/tutorial/arrays/