简体   繁体   中英

Init a 3 dimensional array with 2 dimensional arrays

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM