我是C ++的新手,仅用于学习目的。
我只是了解如何通过辅助int input [][]
将int** output
转换为int* aux []
,如下所示。
int TwoD()
{
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;
}
现在,我想按如下所示将其扩展到3D。
void ThreeD()
{
int input[2][3][4] =
{
{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
},
{
{13,14,15,16},
{17,18,19,20},
{21,22,23,24}
}
};
//int(*output)[3][4] = input;
int** aux[2];
aux[0][0] = input[0][0];
aux[0][1] = input[0][1];
aux[0][2] = input[0][2];
aux[1][0] = input[1][0];
aux[1][1] = input[1][1];
aux[1][2] = input[1][2];
int*** output = aux;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 4; k++)
cout << output[i][j][k] << " ";
cout << endl;
}
cout << endl;
}
}
它可以编译,但只会产生空白屏幕。什么是正确的辅助aux
以及如何对其进行初始化?
答案 0 :(得分:2)
您需要另一层指针。
int input[2][3][4] =
{
{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
},
{
{13,14,15,16},
{17,18,19,20},
{21,22,23,24}
}
};
int* aux1[2][3] =
{
{ input[0][0], input[0][1], input[0][2] },
{ input[1][0], input[1][1], input[1][2] },
};
int** aux2[2] = {aux1[0], aux1[1]};
int*** output = aux2;