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.

Recommended Answers

All 4 Replies

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.

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

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

Nope. That's the problem code he started with.

Thanks Dave, easy to understand and straight to the point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.