Jackvile Jackvile - 29 days ago 17
C Question

Explain why I get: warning assignment from incompatible pointer type

I'm trying to make a tic tac toe program, but I have run into a warning. I know that the code still compiles, but I want to know why I have this warning and what I can do to fix it.

I get the warning when p = a;

void clean(){
char a[N][N], *p;
p = a;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
*p++ = '_';
}
}
display(a);
}
void display(char a[N][N]){
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%c ", a[i][j]);
}
printf("\n");
}
}

Answer

Your clean function should be this:

void clean(void){
    char a[N][N];

    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            a[i][j] = '_';
        }
    }
    display(a);
}

There is absolutely no advantage of having the p pointer, except that your code is less readable.