考虑这段代码:
#define MAX 4
............
............
int** ptr = (int**)malloc(sizeof(int*)*MAX);
*ptr = (int*)malloc(sizeof(int)*MAX);
// Assigned values to the pointer successfully
如何以 ptr
作为参数调用 foo()
? foo()
的原型(prototype)声明如下:
void foo(int arr[][MAX]);
最佳答案
您不能将 ptr
作为参数传递给 foo
函数。
二维数组(数组的数组)的内存布局与指向数组的指针数组的内存布局大不相同。
要么更改函数签名以接受 int**
(可能还有大小信息),要么将 ptr
定义为适当大小的二维数组.
关于c - 如何将 "pointer to a pointer"传递给需要 "pointer to array"的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16395968/