Only compiles as an array of pointers, not an array of arrays

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

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

Is there a way to define a multidimensional array containing 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 to compile is for the compiler to treat arrays as pointers:

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

Is this really the only way to do this, or can I somehow save the type (in C ++, information like the runtime will know that it is an array) and consider it combineas a multidimensional array? I understand that this works because the array name is a const pointer, but I'm just wondering if there is a way to do what I ask in standard C / C ++, rather than relying on compiler extensions. Maybe I'm too used to Python lists, where I could just throw something into them ...

+3
source share
1 answer

No. The first is

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

It can not work because arr1, and arr2can not be used to initialize the array. But this:

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

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

works just like this one

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

char const *combine[][2] = { {"i", "j"}, {"m", "n"} };
+6

Source: https://habr.com/ru/post/1745381/


All Articles