我想以只读模式将2D指针/数组传递给函数。我试过一个1D的情况,它按照我想要的方式工作,但是对于2D数组它没有!
错误消息是
cannot convert argument 1 from 'int**' to 'const int **'
我的代码在这里
void test_constdpointer(int const** pda){
cout << "function: \n";
for (int i = 0; i < 3; i++) {
cout << pda[i][0] << " " << pda[i][1] << " " << pda[i][2] << "\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int **da;
da = new int*[3];
for (int i = 0; i < 3; i++) {
da[i] = new int[3];
}
for (int i = 0; i < 3; i++) {
cout << da[i][0] << " " << da[i][1] << " " << da[i][2] << "\n";
}
test_constdpointer(da);
return 0;
}
答案 0 :(得分:3)
您需要按const
两个指针级别。
void test_constdpointer(int const* const* pda)