C++ - Too Many Initializers for Arrays

49,166

Solution 1

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.

Solution 2

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

Solution 3

You mixed the 6 and the 9 in the indexes.

Share:
49,166
Xelza
Author by

Xelza

Updated on October 22, 2020

Comments

  • Xelza
    Xelza over 3 years

    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}};
    
  • chris
    chris over 11 years
    @David, The indices are swapped.
  • Rapptz
    Rapptz over 11 years
    Ah you were faster than me. +1
  • Xelza
    Xelza over 11 years
    wow, thank you, that was simple enough but I haven't noticed it -_-