I am a newbie who is learning C++ at random order.
In the following first three cases I can digest what is going on because the pattern of implicit decay is clear.
"an array of
X
" is implicitly decayed to "a pointer toX
".
void case1()
{
int a[] = { 1,2,3,4,5,6 };
int *b = a;// array of int ---> pointer to int
}
void case2()
{
int input[][3] = { {1,2,3},{4,5,6} };
int(*output)[3] = input;// array of int[] ---> a pointer to int[]
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
cout << output[i][j] << endl;
}
int case3()
{
int input[][3] = { {1,2,3},{4,5,6} };
int* aux[2];
aux[0] = input[0];// array of int ---> pointer to int
aux[1] = input[1];// array of int ---> pointer to int
int** output = aux;// array of int* ---> pointer to int*
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
cout << output[i][j] << endl;
}
Question
However, I am really confused with the fourth case as follows.
int case4()
{
int input[][3] = { {1,2,3},{4,5,6} };
int* aux[2];
aux[0] = (int*)input;// array of int[] ---> pointer to int
aux[1] = aux[0] + 3;
int** output = aux;// array of int* ---> pointer to int*
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
cout << output[i][j] << endl;
}
How can "an array of int[]
" can be explicitly decayed to "a pointer to int
"?
aux[0] = (int*)input;// array of int[] ---> pointer to int
Any easy explanations are welcome!