C ++ - 数组的初始化程序太多

时间:2012-09-09 01:07:05

标签: c++ c multidimensional-array initialization

我已经制作了这样的数组但是它一直说我有太多的初始化器。我该如何解决这个错误?

        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 个答案:

答案 0 :(得分:9)

这里的问题是你在数组声明部分中交换了行/列索引,因此混淆了编译器。

通常在声明多维数组时,第一个索引用于行,第二个索引用于列。

此表单应修复它:

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

答案 1 :(得分:3)

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

答案 2 :(得分: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},
};

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