Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment from incompatible pointer type after code compilation

Tags:

c

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.

like image 216
Fokwa Best Avatar asked Nov 30 '25 15:11

Fokwa Best


2 Answers

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 */
like image 186
David Ranieri Avatar answered Dec 02 '25 05:12

David Ranieri


you want char *names[11] array of char pointers, not char *names[][11].

Declare it like

char *names[] = {"Drew", "Larry Page", "Seggy", "Mark"};
like image 20
Achal Avatar answered Dec 02 '25 04:12

Achal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!