I get an "incompatible pointer type" and I don't understand why

I get two types of errors:

Compiler Complaints

pr.c: In the main function:

pr.c: 20: 2: warning: pass argument 1 of 'printMatrix from an incompatible pointer type [enabled by default]

pr.c: 9: 6: note: expected 'const int (*) [80], but the argument is of type' int (*) [80]

pr.c: 22: 2: warning: passing argument 1 of "lights from an incompatible pointer type [enabled by default]

pr.c: 10: 6: note: expected 'const int (*) [80], but the argument is of type' int (*) [80]

The compiler seems to be complaining about getting a non-const in a function that accepts const, but I was told that this is the correct way to use const...

#include <stdio.h>
#include <stdlib.h>

#define MAXCOL  80
#define MAXROW  20
#define randNormalize() srand(time(0))

void fillMatrix(int m[][MAXCOL], size_t rows, size_t cols);
void printMatrix(const int m[][MAXCOL], size_t rows, size_t cols);
void lights(const int m[][MAXCOL], size_t rows, size_t cols);
int star(const int m[][MAXCOL], int row, int col);

int main()
{
    int m[MAXROW][MAXCOL];

    randNormalize();

    fillMatrix(m, 5, 5);
    printMatrix(m, 5, 5);

    lights(m, 5, 5);

    return 0;
}

void fillMatrix(int m[][MAXCOL], size_t rows, size_t cols)
{
    int i, j;

    for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++)
            m[i][j] = rand()%21;

}

void printMatrix(const int m[][MAXCOL], size_t rows, size_t cols)
{
    int i, j;

    for(i = 0; i < rows; i++)
    {
        printf("\n");

        for(j = 0; j < cols; j++)
            printf("%d ", m[i][j]);
    }

    printf("\n");
}


void lights(const int m[][MAXCOL], size_t rows, size_t cols)
{
    int i, j;

    for(i = 1; i < rows - 1; i++)
    {
        printf("\n");

        for(j = 1; j < cols - 1; j++)
        {
            if( star(m, i, j) )
                printf("*");
            else
                printf(" ");
        }
    }

    printf("\n");
}



int star(const int m[][MAXCOL], int row, int col)
{
    int i, j;
    int sum = 0;

    for(i = row - 1; i <= row + 1; i++)
        for(j = col - 1 ; j <= col + 1; j++ )
            sum += m[i][j];

    return (sum/9 > 10);
}

, , , ( ).

+1
2

, C int[X][Y] const int[X][Y]. int (*)[Y] const int (*)[Y].

; , . (++ ).

, :

  • int const int
  • const int, . printMatrix((const int (*)[MAXCOL])m, 5, 5);

1, const-correctness .

+2

m , , . , m int (*)[5] - " 5 int s".

fillMatrix, int (*)[MAXCOL], .. int (*)[80] - " 80 ints".

.

, , , , , , .

2 4.

int m1[2][2];

:

m1[0][0]    m1[1][1]
|           |
+---+---+---+---+
|   |   |   |   |
+---+---+---+---+

,

int (*p1)[2] = m1;

:

p1      p1+1    p1+2
|       |       |
v       v       v
+---+---+---+---+
|   |   |   |   |
+---+---+---+---+

4 x 4 , .

int m2[4][4];

:

m2[0][0]                                                    m2[3][3]
|                                                           |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

:

int (*p2)[4] = m1;

:

p2              p2+1            p2+2            p2+3            p2+3
|               |               |               |               |
v               v               v               v               v
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

, , , :

p1[1][0] p2[1][0] , → int.

p1 , p2, undefined.

int (*)[4], int (*)[80].

. -

int m[2][MAXCOL];
+1

Source: https://habr.com/ru/post/1663938/


All Articles