I have some problems using two dimensional array in the code and need some help.
static const int PATTERNS[20][4];
static void init_PATTERN()
{
// problem #1
int (&patterns)[20][4] = const_cast<int[20][4]>(PATTERNS);
...
}
extern void UsePattern(int a, const int** patterns, int patterns_size);
// problem #2
UsePattern(10, PATTERNS, sizeof(PATTERNS)/sizeof(PATTERNS[0]));
in the first statement, I need to cast the const off the two dimensional array PATTERNS. The reason for this is that the init function is called only once, and in the remaining code, PATTERNS is strictly read-only.
In the second statement, I need to pass PATTERNS array to the int** argument. Direct passing resulted a compile error.
Thanks!
======================
I have solved the problem, just about the same time that Andrey posted the answer. Yes int[][] can't be casted to int**
.
It can be casted to int*
through &(PATTERNS[0][0])
, and the function prototype must be modified with row size (the number of elements in a row). The array can be const_cast away with reference syntax.