C语言 警告:应为“int **”,但参数的类型为“int (*)[(sizetype)(n)]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20297594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:25:06  来源:igfitidea点击:

Warning: Expected ‘int **’ but argument is of type ‘int (*)[(sizetype)(n)]’

c

提问by kamalbanga

Following code:

以下代码:

#include <stdio.h>

void printSpiral(int **M, int row1, int row2, int col1, int col2) {
  if (!((row1 <= row2) && (col1 <= col2)))
    return;
  int i;
  for (i = col1; i <= col2; i++)
    printf("%d ", M[row1][i]);
  for (i = row1; i <= row2; i++)
    printf("%d ", M[i][col2]);
  for (i = col2; i >= col1; i--)
    printf("%d ",M[row2][i]);
  for (i = row2; i >= row1; i--)
    printf("%d ",M[i][col1]);
  printSpiral(M,row1+1,row2-2,col1+1,col2-1);
}

int main() {
  int n;
  scanf("%d",&n);
  int M[n][n];
  int i, j;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      scanf("%d",&M[i][j]);
  printSpiral(M,0,n-1,0,n-1);
  return 0;
}

gives following warning:

给出以下警告:

spiral.c: In function ‘main':

spiral.c:26:3: warning: passing argument 1 of ‘printSpiral' from incompatible pointer     
type [enabled by default]
printSpiral(M,0,n-1,0,n-1);`

spiral.c:3:6: note: `expected ‘int **' but argument is of type ‘int (*)[(sizetype)(n)]'`
void printSpiral(int **M, int row1, int row2, int col1, int col2) {

I have seen this warning for the first time. What does it mean?

我第一次看到这个警告。这是什么意思?

回答by haccks

There are two problems with this code. First you are passing argument of int (*)[(sizetype)(n)](pointer to an array of nintegers, this is the type your 2D array Mdecays to when you pass it to a function) to your function which is expecting an argument of int **. Always remember Arrays are not pointers. One possible solution is you can change your function's first parameter to int (*)[(sizetype)(n)]type.

这段代码有两个问题。首先,您将参数 of int (*)[(sizetype)(n)](指向n整数数组的指针,这是您的二维数组M在您将其传递给函数时衰减到的类型)传递给您的函数,该函数需要int **. 永远记住数组不是指针。一个可能的解决方案是您可以将函数的第一个参数更改为int (*)[(sizetype)(n)]type。

void printSpiral(int (*M)[n], int row1, int row2, int col1, int col2) {  

But by doing this the second problem will comes into light and that is because of you declared Mas variable length array and nis not known to the function. This issue can be resolved by passing nfrom mainto your function and hence change your function definition to

但是通过这样做,第二个问题就会暴露出来,这是因为您声明M为可变长度数组并且n函数不知道。这个问题可以通过将解决nmain你的函数,因此你的函数定义修改为

void printSpiral(int n, int (*M)[n], int row1, int row2, int col1, int col2) {

回答by glglgl

The compiler wants to tell you that the functions expects a int **, but you provide it an argument of type int (*)[(sizetype)(n)].

编译器想告诉您函数需要 a int **,但您为它提供了一个类型为 的参数int (*)[(sizetype)(n)]

The first is a pointer to (the first element of an array of) int *, the second one is a pointer to an array/arrays of given length.

第一个是指向(数组的第一个元素)int *的指针,第二个是指向给定长度的数组的指针。

Compare what both means in memory and why this cannot work this way.

比较两者在内存中的含义以及为什么不能以这种方式工作。

The solution would be to provide the function a pointer to one element an at least the total number of columns. (If you want the function to do bounds-checking, it needs the number of rows as well.) Then you have to calculate the indexes manually.

解决方案是为函数提供一个指向一个元素的指针,至少是总列数。(如果你想让函数做边界检查,它也需要行数。)然后你必须手动计算索引。

回答by sundq

you should know int a[][]is different from int **a,the first ais same to int (*a)[] which means a pointer which point a array;if you want to indicate a 2D array by int **a,please reference this:C Programming: Initialize a 2D array of with numbers 1, 2, 3

你应该知道int a[][]不同于int **a,第a一个与 int (*a)[] 相同,这意味着一个指向数组的指针;如果你想用 指示一个二维数组int **a,请参考这个:C 编程:初始化一个二维数组数字 1, 2, 3