C - why is strcpy() necessary

Arrays in C are non-assignable and non-copy-initializable. That's just how arrays are in C. Historically, in value context (on the RHS of assignment) arrays decay to pointers, which is what formally prevents assignment and copy-initialization. This applies to all arrays, not only to char arrays.

C language inherits this arrays behavior from its predecessors - B and BCPL languages. In those languages arrays were represented by physical pointers. (And obviously re-assignment of pointers is not what you'd want to happen when you assign one array to another.) In C language arrays are not pointers, yet they do "simulate" the historical behavior of B and BCPL arrays by decaying to pointers in most cases. This historical legacy is what keeps C arrays non-copyable to this day.

One exception from the above is the initialization with a string literal. I.e. you can do

char c[] = "abc";

in which case conceptually we are copying string literal "abc" to array c. Another exception is array wrapped into a struct type, which is copied when the whole struct object is copied. And that's about it.

This means that whenever you want to copy a naked (non-wrapped) array, you have to use a library-level memory copying function, like memcpy. strcpy is just a flavor of that specifically tailored to work with strings.


That's simply what arrays are in C. You can't assign to them. You can use pointers if you like:

char *p;
p = "abc";

Incidentally, there is a C FAQ.

Arrays are ``second-class citizens'' in C; one upshot of this prejudice is that you cannot assign to them.


Short answer: historical reasons. C never had a built in string type. It wasn't until C++ came along that std::string came into being, and even that did not arrive with the first implementations

Long answer: the type of "abc" is not char[], but rather char *. strcpy is one mechanism with which you can copy the data that the pointer points at (in this case that's ABC).

strcpy isn't the only way to initialize an array, however, it is smart enough to detect and respect the terminating 0 at the end of the string. You could also use memcpy to copy the string into s but that requires you pass in the length of the data to be copied, and to ensure the terminating 0 (NULL) is present in s