I'm trying to understand something about 2d char arrays. How can I set up my 2d array to accept the following

char a[][] = {{"item1", "1", "1"}, {"item2", "1", "2"}}

no matter what values I put in 2 brackets there are too many initializers, and putting a third dimension gives me a lvalue error

This call works if I make "a" a pointer, but if I made a a pointer all the data is written into read only memory and becomes unmodifiable.

For now i've simply taken out the inner curly braces and made it one long stream. That seems to have made it happy, however I dont' want to leave this alone until I understand why this particular route wasn't working.

Thanks greatly in advance.

7 Years
Discussion Span
Last Post by MaestroRage

A 2D array of chars is a 1D array of strings, so to speak. So if you want a 2D array of modifiable strings, you'll need a 3D array of chars.

char a[][3][6] = {{"item1", "1", "1"}, {"item2", "1", "2"}};

I think what you are looking for is something like this :

char *twoDee[][3] = {{"item1", "1", "1"}, {"item2", "1", "2"}};

You need to supply the column before hand.
Thus the code :

char *p[][] = {{"item1", "1", "1"}, {"item2", "1", "2"}};

Is an error because the compiler doesn't know the number of column before runtime.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.