警告:预期'诠释**“,但参数的类型为”INT(*)[(sizetype)(N)]“ [英] Warning: Expected ‘int **’ but argument is of type ‘int (*)[(sizetype)(n)]’

查看:350
本文介绍了警告:预期'诠释**“,但参数的类型为”INT(*)[(sizetype)(N)]“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继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?

推荐答案

有两个问题code。首先要传递为int的参数(*)[(sizetype)(N)] (指针数组 N 整数,这是该类型的二维数组 M 衰变,当你把它传递给一个函数)到你的函数,它期待 int的参数来** 。永远记住 数组不是指针。一个可能的解决方案是,你可以改变你的函数的第一个参数 INT(*)[(sizetype)(N)] 键入。

There are two problems with this code. First you are passing argument of int (*)[(sizetype)(n)] (pointer to an array of n integers, this is the type your 2D array M decays 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.

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

但是,这样做的第二个问题将进入光,那是因为你声明 M 可变长度数组和 N 是不知道的功能。这个问题可以通过传球来解决 N 给你的函数,因此你的函数定义修改为

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

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

这篇关于警告:预期'诠释**“,但参数的类型为”INT(*)[(sizetype)(N)]“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆