assignment from incompatiable pointer type C -
say want read in list of pages name of max 19 character, e.g. 4 (number of page) name1 name2 name3 name4
i trying use global 2d array store page number , page name, got error saying assignment incompatiable pointer type...
thanks
static int npages; static char** pagename; int main(void){ scanf(" %d", &npages); pagename = (char *)malloc(npages*sizeof(char)); for(int i=0; < npages ;i++){ pagename[i] = (char *)malloc(20*sizeof(char)); scanf(" %s", pagename[i]); } //free memory here of coz. return 0; }
the problem lies right there:
pagename = (char *)malloc(npages*sizeof(char));
pagename char **
, not char *
. should read:
pagename = malloc(npages*sizeof(char*)); // sizeof(char *), , no need cast
edit: removed cast
Comments
Post a Comment