What is the difference Between those types of initializing An array called "IntegerArray" With 5 element :

int IntegerArray[5] = {0};
     int IntegerArray[5] = {0,0,0,0,0};
     int IntegerArray[5] = {10,20,30,40,50};
     int IntegerArray[]  = {10,20,30,40,50};

Please i need an obvious explanation for each line as i am confused.....

Thanks for reading...............

7 Years
Discussion Span
Last Post by Dimesh

1.) Every element is assigned the value zero
2.) Accomplishes the same thing
3.) No different than 2.)
4.) Since you are defining the array you don't need to tell the compiler how big it is


Just to repeat whats been said again.

int IntegerArray[5] = {0};
initializes all elements inside the array to 0.

int IntegerArray[5] = {0,0,0,0,0};
Here you are explicitly initializing each element to 0.

int IntegerArray[5] = {10,20,30,40,50};
Here you are explicitly giving each element inside the array a value.
So IntegerArray[0] has 10 stored, IntegerArray[1] has 20, and so on.

int IntegerArray[] = {10,20,30,40,50};
Here noticed that there is no 5 like before. By putting no number, and just using the
array brackets,[], you tell the compiler that the size of the array is the number
of elements inside the array. Since this "{10,20,30,40,50" has 5 element, the compiler deduces that IntegerArray has 5 elements.

Edited by firstPerson: n/a


Thanks jonsca,firstPerson ..... That was Helpful

firstPerson, you explain good, i hope better for you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.