在这个例子中:
int a[2][2]={{1,2},{3,4}};
int *p=a[0];
cout<<p;
cout<<&a[0][0];
两者给出相同的输出。 那为什么我不能像这样调用函数(比如有趣)并循环遍历数组:
fun(a[0]);
fun(int *p)
{
cout<<p[1][1];
}
最佳答案
fun(a[0]); //this looks OK
void fun(int *p) // this is OK if you add return type'
^^^^
{
cout<<p[1][1]; //NOT OK! You can't have 2 indices on an `int*`
cout << p[1]; // OK, will print a[0][1]
}
关于c++ - 将二维数组作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6648828/