Matrix, pointers, C*

后端 未结 3 2134
日久生厌
日久生厌 2021-01-25 00:53

I have code like this:

void print_matrix(int **a, int n) {
    int i, j;
    for(i = 0; i < n; i++) {
        for(j = 0; j < n; j++)
            printf(\"%         


        
3条回答
  •  没有蜡笔的小新
    2021-01-25 01:24

    int** is a pointer to a pointer (pointing on an int).

    int[3][3], as a function argument, is converted to a pointer to an int array - see Is an array name a pointer?

    So the types don't match, as the compiler is telling you.

    Note: if you're doing pointer arithmetic in your function, you can pass int *a instead of int **a (by casting: print_matrix((int *)matrix, 3);. That's ugly but helps to understand what's going on - namely, a int[3][3] array is stored in memory exactly as a int[9] array, and if you're computing the int positions yourself, as you do, it will also work as a 1D array.

提交回复
热议问题