C ++ - too many initializers for arrays

I created such an array, but then it says that I have 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}}; 
+6
source share
3 answers
 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 order lines, and then in columns, so in the initializer for the array you defined, there are 6 lines of 9 integers, not 9 lines of 6 integers.

+2
source

The problem here is that you have the row / column indexes replaced in the array declaration part, and therefore the compiler is confused.

Typically, when declaring a multidimensional array, the first index is for rows, the second is for columns.

This form should fix:

  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}}; 
+8
source

You mixed 6 and 9 in the indices.

+3
source

Source: https://habr.com/ru/post/924935/


All Articles