C++ - Too Many Initializers for Arrays
I Have Made an Array Like This but Then It Keeps Saying I Had Too Many Initializers. How Can I Fix This Error? Int People[6][9] = {{0,0,0,0,0,0}...
I have made an array like this but then it keeps saying I had too many initializers. How can I fix this error?
int people[6][9] = {{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0}};
3 Answers
The issue here is that you have the rows/columns indices swapped in the array declaration part, and thus the compiler is confused.
Normally when declaring a multi-dimensional array, first index is for rows, second is for columns.
This form should fix it:
int people[9][6] = {{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0}};
int people[6][9] =
{
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
};
Arrays in C are in the order rows then columns, so there are 6 rows of 9 integers, not 9 rows of 6 integers in the initializer for the array you defined.
You mixed the 6 and the 9 in the indexes.