I am using the function strcpy()
a lot for an assignment. Looking at the function prototype for strcpy()
:
char *strcpy(char *dest, const char *src)
It can be seen that strcpy()
takes in two pointers.
However, I have seen in a lot of examples online as well as in my own code that a string can be copied in the following manner and it will work:
char array[30];
strcpy(array, "mystring");
printf("%s\n", array);
My question is, why does strcpy()
work if the second parameter that you're passing is a string? Wouldn't the second parameter need be a pointer to a string? E.g. wouldn't you have to do the following:
const char *str = "mystring";
char array[30];
strcpy(array, str);
printf("%s\n", array);
I'm confused about why you can directly pass a string as the second parameter to strcpy()
and not a pointer to the string. Any insights would be really appreciated.