format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[64]

一笑奈何 提交于 2019-12-17 19:52:01

问题


I'm creating a program using C, and I have this line in my code :

scanf("%s", &path);

When I compile the source file, I get this warning :

main.c:84:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[64]’ [-Wformat]

And this is the declaration for the path variable :

char path[64];

Why I'm seeing this error? And how can I solve it ?


回答1:


An array is already a pointer-like object (as dreamlax points out). You don't need the & operator, since declaring

char path[64];

is equivalent to setting path to a pointer to a 64-byte region of memory.




回答2:


The %s format specifier requires you to supply a char *, which is a pointer to char. You are passing &path, which is a pointer to an array. You can just pass path by itself, which will evaluate to a pointer to the first element of the array (the same as &path[0]).




回答3:


try this scanf("%s", path); instead because I think path is an array and a pointer to an array is the array name itself ( array == &array )



来源:https://stackoverflow.com/questions/19439525/format-s-expects-argument-of-type-char-but-argument-2-has-type-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!