Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0},
                        {0,0,0,0,0,0}};
like image 541
Xelza Avatar asked Sep 09 '12 01:09

Xelza


People also ask

What does too many initializers mean in C?

too many initializers. The number of initializers exceeds the number of objects to be initialized.

What happens when an array is initialized with more Initializers as compared to its size?

An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize.

What is an initializer C?

Initializer. In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or a comma-separated list of expressions placed in curly brackets (braces).


1 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}};
like image 113
TheAJ Avatar answered Oct 06 '22 13:10

TheAJ