I have been learning C programming lately and writing some codes. Below is a code I have written:
#include <stdio.h>
int main(int argc, char ** argv){
int num[] = {10, 15, 25, 30, 45, 10};
char *names[][11] = {"Drew", "Larry Page", "Seggy", "Mark"};
//int *ip, i = 0;
char **ip;
int *p, i = 0, j = 0;
for (ip = names; *(ip + i); i++)
printf("%s ", *(ip + i));
printf("\n\n");
for (p = num; j < sizeof(num)/sizeof(num[0]); j++)
printf("%d ", *(p + j));
printf("\n");
return 0;
}
When I build and run the above code, I get my desired result. However I get this warning:
jdoodle.c:12:13: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
for (ip = names; *(ip + i); i++)
^
I have tested the code on various IDE's but get similar warning. Appreciate assistance.
The problem is in this line:
char *names[][11] = {"Drew", "Larry Page", "Seggy", "Mark"};
you don't want a 2d array of pointer to char, you want an array of pointer to char:
char *names[] = {"Drew", "Larry Page", "Seggy", "Mark"}; /* Don't hardcode 11 */
you want char *names[11] array of char pointers, not char *names[][11].
Declare it like
char *names[] = {"Drew", "Larry Page", "Seggy", "Mark"};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With