When i try to assign values to an array like this:
int ar[5] = {18,19,20,21,22,23};
I get a compiler error: too many initializers for 'int [5]'
But when I do it like this:
int ar[5];
ar[0] = 12, ar[1] = 13, ar[2] = 14, ar[3] = 15, ar[4] = 16, ar[5] = 17;
Everthing is fine and the program runs fine and outputs correct results, am I doing something wrong or?
int ar[5]means array of size 5. The indices will be from 0 to 4 only.{18,19,20,21,22,23}contains 6 numbers, and compiler was expecting 5 numbers. in case ofar[5] = 17, since cpp doesn't check for limits/size of array, it just puts17at the calculated memory address...