M Li
M Li

Reputation: 13

c++: Arrays, too many initializers

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;
int main(){
char somearray[6][5] = {{'M','a','r', 't', 'i', 'n'},
                        {'L','i','a','m','z'}};

for(int j=0; j<5; j++ ){
    for (int k = 0; k<5; k++ ){
        cout<< somearray[j][k];
    }
}



return 0;
}

error:

 test.cpp: In function ‘int main()’:
 test.cpp:11:29: error: too many initializers for ‘char [5]’
     {'L','i','a','m','z'}};

There is something I don't understand, I have one error, I tried to mess with the multi dimensional array initializer however I keep getting the same too many initalizers error. I followed a c++ tutorial and I keep getting that error. I don't understand.

Upvotes: 1

Views: 1028

Answers (2)

MikeCAT
MikeCAT

Reputation: 75062

Unfortunately, your compiler seems a little stupid.

{'L','i','a','m','z'} is ok, but {'M','a','r', 't', 'i', 'n'} is too long for char[5].

Upvotes: 0

vsoftco
vsoftco

Reputation: 56547

Your first element somearray[0] has 6 elements

{'M','a','r', 't', 'i', 'n'}

instead of 5. Remember that somearray[6][5] declares a bi-dimensional array with 6 rows and 5 columns, or, equivalently, an arrray of 6 arrays of char[5]. The error seems to indicate your last element, but it actually indicates the end of the array definition.

Upvotes: 4

Related Questions