c - Passing pointer to string, incompatible pointer type -
so i'm sure question answered many times having trouble seeing how fix situation. took snippet of program contains warning-generating code:
#include <stdio.h> #include <stdlib.h> inputdata(int size, char *storage[size]) { int iterator = -1; { iterator++; *storage[iterator] = getchar(); } while (*storage[iterator] != '\n' && iterator < size); } main() { char name[30]; inputdata(30, name); }
the warning message:
$ gcc text.c text.c: in function ‘main’: text.c:18:5: warning: passing argument 2 of ‘inputdata’ incompatible pointer type [enabled default] inputdata(30, name); ^ text.c:4:1: note: expected ‘char **’ argument of type ‘char *’ inputdata(int size, char *storage[size])
edit:
ok, managed play around syntax , fixed problem. still wouldn;t mind hearing more knowledgeable why precisely needed. here changed:
#include <stdio.h> #include <stdlib.h> inputdata(int size, char *storage) // changed point string { int iterator = -1; { iterator++; storage[iterator] = getchar(); // changed pointer string } while (storage[iterator] != '\n'); // same above } main() { char name[30]; inputdata(30, name); printf("%s", name); // added verification }
array name converted pointer first element when passed function. name
converted &name[0]
(pointer char
type) address of first element of array name
. therefore, second parameter of function must of pointer char
type.
char *storage[size]
equivalent char **storage
when declared function parameter. therefore, change char *storage[size]
char *storage
.
Comments
Post a Comment