如何将const多维数组传递给函数?

时间:2015-03-28 22:21:56

标签: c gcc

有什么问题?如果我把'const'放在我的二维数组的函数声明中,为什么GCC会抱怨?

#include <stdio.h>

void print_array(const int array[][5], int n, int m);

int main(void)
{
    int A[2][5] = {{1,2,3,4,5}, {1,2,3,4,5}};
    print_array(A, 2, 5);
    return 0;
}

void print_array(const int array[][5], int n, int m)
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            printf("%d\n", array[i][j]);
        }
    }
}

GCC输出:

asd.c: In function ‘main’:
asd.c:8:2: warning: passing argument 1 of ‘print_array’ from incompatible pointer type [enabled by default]
  print_array(A, 2, 5);
  ^
asd.c:3:6: note: expected ‘const int (*)[5]’ but argument is of type ‘int (*)[5]’
 void print_array(const int array[][5], int n, int m);
      ^

0 个答案:

没有答案