I'm getting two types of errors:
Compiler's complaints
pr.c: In function ‘main’:
pr.c:20:2: warning: passing argument 1 of ‘printMatrix’ from incompatible pointer type [enabled by default]
pr.c:9:6: note: expected ‘const int (*)[80]’ but argument is of type ‘int (*)[80]’
pr.c:22:2: warning: passing argument 1 of ‘lights’ from incompatible pointer type [enabled by default]
pr.c:10:6: note: expected ‘const int (*)[80]’ but argument is of type ‘int (*)[80]’
It seems the compiler complains about receiving a non-const in a function that takes a const, but I was told this was the correct way of using 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);
}
I'm looking for the best solution that doesn't use pointers, as this was from an exercise of a course in which we have not yet covered them (although I have studied them).
const
-thing is fine, butfillMatrix
expects a pointer to array of 80int
s, while you pass a pointer to an array of 5. Those are not compatible. This question might explain more. – Kninnug