3

What is wrong? Why GCC complains if I put 'const' in function declaration for my two dimension array?

#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 output:

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);
      ^
  • Those are only warnings, you can ignore them or read this: http://stackoverflow.com/questions/29323297/how-to-pass-a-const-multi-dimension-array-to-a-function –  Mar 28 '15 at 22:52
  • @cremno that q/a is a complete mess, let's not link to it – M.M Mar 28 '15 at 23:24

0 Answers0