1
votes

Sorry if this may seem like a stupid question.

I had this code:

char array1[2];
char array2[2];
char array3[2];

char *array[] = {
array1,
array2,
array3,
}

Now I changed array1,array2,array3 to be a 2 dimensional array:

char array1[2][2];
char array2[2][2];
char array3[2][2];

And I get the following error: error: cannot convert 'char (*)[2]' to 'char*' in initialization

How can I init array with 2 dimensional array?

I also tried the following which didn't work:

char *array[2][2] = {
    array1,
    array2,
    array3,
    }

char array[][2][2] = {
    array1,
    array2,
    array3,
    }

Thanks!

1

1 Answers

2
votes

C++11 makes this easy:

char array1[2][2];
char array2[2][2];
char array3[2][2];


std::decay<decltype(array1)>::type array[] = {
  array1,
  array2,
  array3,
};

which is the equivalent of the (unreadable in my opinion) syntax

char (* array [])[2] = {
  array1,
  array2,
  array3,
};

The rules, as usual, are the same you can find in every decent C++ book:

  • Operator precedence
  • Type decaying