I have the following two similar codes Snippet 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define W 5
#define N 9
void print_w(const char x[W] [N+1]);
int main()
{
char words[W][N + 1]={{}};
print_w(words);
printf("R*E*V*E*R*E*S*E\n");
print_w( (const char (*) [N+1]) words);
return 0;
}
void print_w(const char x[W] [N+1]){
int i;
for (i=0; i<W; i++) {
printf("Word (%d) :%s\n", i, x[i]);
}
return;
}
Snippet 2:
#include <stdio.h>
#include <stdlib.h>
#define N 9
void printString(const char []);
int main(){
char mystr[N + 1];
scanf("%9s", mystr);
printString(mystr);
printString( (const char (*) ) mystr);
return 0;
}
void printString(const char str[]){
int i;
for (i=0; str[i]!=0; i++) {
printf("%c\n", str[i]);
}
}
In snippet 1 I have a warning telling me that in line print_w(words);
:
[Warning] passing argument 1 of 'print_w' from incompatible pointer type
Implying that I have to make a typecast in print_w( (const char (*) [N+1]) words);
While in snippet 2 this is not the case with my code as I have performed both options without warnings.
Why there is a warning in the first example and not in the second? I suppose that passing wrong variable type (char --> const) in the function is not the case because a warning will occured also in snippet 2.