3

Suppose I define two arrays, each of which have 2 elements (for theoretical purposes):

char const *arr1[] = { "i", "j" };
char const *arr2[] = { "m", "n" };

Is there a way to define a multidimensional array that contains these two arrays as elements? I was thinking of something like the following, but my compiler displays warnings about incompatible types:

char const *combine[][2] = { arr1, arr2 };

The only way it would compile was to make the compiler treat the arrays as pointers:

char const *const *combine[] = { arr1, arr2 };

Is that really the only way to do it or can I preserve the type somehow (in C++, the runtime type information would know it is an array) and treat combine as a multidimensional array? I realise it works because an array name is a const pointer, but I'm just wondering if there is a way to do what I'm asking in standard C/C++ rather than relying on compiler extensions. Perhaps I've gotten a bit too used to Python's lists where I could just throw anything in them...

1
  • Interesting problem. I don't know the solution, but I'll be watching. Commented May 14, 2010 at 1:19

1 Answer 1

6

No. First, this

char const *combine[][2] = { arr1, arr2 };

cannot work, because arr1 and arr2 cannot be used to initialize an array. But this:

char const *arr1[] = { "i", "j" };
char const *arr2[] = { "m", "n" };

char const *(*combine[])[2] = { &arr1, &arr2 };

works as well as this

char const *arr1[] = { "i", "j" };
char const *arr2[] = { "m", "n" };

char const *combine[][2] = { {"i", "j"}, {"m", "n"} };
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting solution... An array of pointers to arrays each of length 2 containing values of type char const * (or const char * if you prefer; they're the same thing). The extra level of indirection is a bit of a nuisance when I want to use the arrays contained in combine, but I like the idea because it does exactly what I asked! Not only that, you showed how to do it generically with any arrays of the same length. Thanks a bunch! ^_^
In const char *arr1[], arr1 is an array of pointers to const char, in char *const arr1[], arr1 is an array of const pointers to char, but char const *arr1[]... Is it a compiler extension?
Notice that you have to access the arrays as combine[i][0][j] or (*combine[i])[j] in the first case. In the second case it's just combine[i][j]. Another interesting fact is that in C you don't even need to specify the 2, because T[] is compatible with T[N]. You can just say char const *(*combine[])[] = { &arr1, &arr2 };. However you can then only use the (*combine[i])[j] form of accessing (because sizeof *combine[i] is unknown). That's a C speciality only, though. In C++ you need to specify the size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.