Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何正确传递包含指针的数组?_C_Arrays_Pointers - Fatal编程技术网

C 如何正确传递包含指针的数组?

C 如何正确传递包含指针的数组?,c,arrays,pointers,C,Arrays,Pointers,我正在写一个代码,用djikstra的算法找到迷宫中的最短路径 当我通过我的阵列时,我得到了 warning "passing argument 1 of 'traverse' from incompatible pointer type" expected 'int (*)[(sizetype)(n)]' but argument is of type 'int **' 我最初在main中初始化并分配了矩阵: int **mat; mat = (int **)malloc(sizeof(i

我正在写一个代码,用djikstra的算法找到迷宫中的最短路径

当我通过我的阵列时,我得到了

warning "passing argument 1 of 'traverse' from incompatible pointer type" 
expected 'int (*)[(sizetype)(n)]' but argument is of type 'int **'
我最初在main中初始化并分配了矩阵:

int **mat;
mat = (int **)malloc(sizeof(int *)*n);
for(i=0; i<n; i++){
  mat[i]=(int *)malloc(sizeof(int)*n);
}
然后我在这里调用main中的这个函数

traverse(mat, 0);

我确信将数组作为指针传递是有问题的,但我对这里的规则不太熟悉。非常感谢您的帮助。

int**mat
是一个“指向int的指针”

int-mat[n][n]
作为一个函数参数,与
int(*mat)[n]
是一个“指向int数组n的指针”

第一个指向指针,第二个指向数组——不同类型


不要将
mat
作为
int mat[n1][n2]
传递到
traverse()
,而是作为
int**
传递。同时传递2个维度

// void traverse(int mat[n1][n2], int src){
void traverse(int **mat, size_t n1, size_t n2, int src) {
...
--

建议更改分配代码

int **mat;
size_t n1 = foo();
size_t n2 = foo();
mat = malloc(sizeof *mat * n1);
for(size_t i=0; i<n1; i++){
  mat[i]= malloc(sizeof *(mat[i]) * n2);
}
int**mat;
大小_t n1=foo();
大小_t n2=foo();
mat=malloc(sizeof*mat*n1);

for(size_t i=0;i
int**mat
是一个“指向int的指针”

int-mat[n][n]
作为一个函数参数,与
int(*mat)[n]
是一个“指向int数组n的指针”

第一个指向指针,第二个指向数组——不同类型


不要将
mat
作为
int mat[n1][n2]
传递到
traverse()
,而是作为
int**
传递。同时传递两个维度

// void traverse(int mat[n1][n2], int src){
void traverse(int **mat, size_t n1, size_t n2, int src) {
...
--

建议更改分配代码

int **mat;
size_t n1 = foo();
size_t n2 = foo();
mat = malloc(sizeof *mat * n1);
for(size_t i=0; i<n1; i++){
  mat[i]= malloc(sizeof *(mat[i]) * n2);
}
int**mat;
大小_t n1=foo();
大小_t n2=foo();
mat=malloc(sizeof*mat*n1);

对于(size\u t i=0;我将进入堆栈溢出)。我现在将详细阅读此内容,谢谢。正如警告所说:
void-traverse(int-mat[n][n],int-src)
-->
void-traverse(int**mat,int-src)
如果评论没有充分地说明问题,那么数组数组并不是指针数组的同义词。它们是不同的野兽。就是这样,谢谢!欢迎使用堆栈溢出!我现在就来读一下,谢谢。正如警告所说:
无效遍历(int-mat[n][n],int-src)
-->
无效遍历(int**mat,int src)
如果评论没有很好地解释问题,那么数组不是指针数组的同义词。它们是不同的野兽。就是这样,谢谢!