Reputation: 19
How to solve matrix problem where values are set of string. I want to use pointer. Following is the warning:
"warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]"
char *country[]={"USA\n","UK\n","Chaina\n","Singapore\n","Scotland\n"};
for (int i =0; i <5; i++) {
printf("\n CHECK val=%s\n",country[i]);
}
char *(*cp)[5]=&country[2];
for (int i =0; i <3; i++) {
printf("\n POINTER val=%s\n",(*cp)[i]);
}
Upvotes: 1
Views: 69
Reputation: 726479
You get the warning because &country[2]
does not return a pointer to an array of five C strings. However, you do not need such a pointer, because a simple char **
pointer will let you create an alias to the middle of an array:
char **cp = &country[2];
for (int i = 0; i < 3 ; i++) {
printf("\n POINTER val=%s\n", cp[i]);
}
Note: You do not need to have \n
characters in both the string literals and the printf
format string, unless you want double-spacing.
Upvotes: 4