When I call strncpy()
with an uninitialized string like this
strncpy(person->name, string, sizeof(person->name));
and then compile with -Wall
and -Werror
, and I see this error:
src/parser.c:18:35: error: argument to ‘sizeof’ in ‘strncpy’ call is the same expression as the destination; did you mean to provide an explicit length? [-Werror=sizeof-pointer-memaccess]
strncpy(person->name, string, sizeof(person->name));
I see that some other posts mentioned array decay in C, is it the same thing in this case? What would be the correct way to use strncpy()
in this function then?
Edit: first, I malloc'd
the struct as well as the string, and then I called strncpy()
. Here is what the struct looks like:
typedef struct {
char *name;
int age;
int phone_num;
} person_t;