error: too many initializers for 'char [4]'

I have these declarations in a program:

int barArray[2][4] = {
{2, 5, -12, 4},
{4, 6, 2993, -1002 }
};
char fooArray[2][4] = {
{"A", "A", "A", "A"},
{"A", "A", "A", "A"}
};

The barArray compiles fine, as it should.
The fooArray is giving me the error message: "error: too many initializers for 'char [4]'"
What is going on?
How can I define a multi dimensional char array [like fooArray] that compiles?

fooArray is an array of characters (e.g. 'A') not strings (e.g. "A")

1 Like

I tried this:

char str[] = "A";
char fooArray[2][4] = {
{*str,*str, *str, *str},
{*str, *str, *str, *str}
};

And it compiles well.

In my real program I've this:

char stations[][2] = {
    {"http://icecast.omroep.nl:80/radio1-bb-mp3", "NPO Radio 1"},
    {"http://icecast.omroep.nl:80/radio2-bb-mp3", "NPO Radio 2"}
    and many more...

So I should make sure that all these strings are '\0' terminated.
Is that what you are saying?

seems that you want an array a strings. try

char* fooArray[2][4] = {

gcjr:
seems that you want an array a strings. try

char* fooArray[2][4] = {

String literals are read-only, the correct type is [b]const[/b] char *fooArray[][2]. You probably don't want to lose the pointers to these strings, so you should probably also make the array itself read-only: [b]const[/b] char *const fooArray[][2].

Pieter

Nice try!
But when I use this declaration:
Code: [Select]

char *stations[9][2] = {
    {"http://icecast.omroep.nl:80/radio1-bb-mp3", "NPO Radio 1"},
    {"http://icecast.omroep.nl:80/radio2-bb-mp3", "NPO Radio 2"},

I get just as many: "warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]" as there are elements in the array.
So, it's not as easy as that.

const char *stations[9][2] = {...
It is!
Thanks all.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.