Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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 ?

like image 434
user2874861 Avatar asked Oct 18 '13 00:10

user2874861


3 Answers

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.

like image 169
Christian Ternus Avatar answered Nov 08 '22 22:11

Christian Ternus


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]).

like image 40
caf Avatar answered Nov 08 '22 23:11

caf


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 )

like image 24
Farouq Jouti Avatar answered Nov 08 '22 22:11

Farouq Jouti