C++ - Too Many Initializers for Arrays

时间:2022-01-22 19:49:03

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 个解决方案

#1


2  

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.

C中的数组按行然后是列,因此在您定义的数组的初始值设定项中有6行9个整数,而不是9行6个整数。

#2


8  

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}};

#3


3  

You mixed the 6 and the 9 in the indexes.

你在索引中混合了6和9。

#1


2  

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.

C中的数组按行然后是列,因此在您定义的数组的初始值设定项中有6行9个整数,而不是9行6个整数。

#2


8  

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}};

#3


3  

You mixed the 6 and the 9 in the indexes.

你在索引中混合了6和9。