Ask HN: Preferred string copy function in C?
4 comments
Depending on your requirements, each has its benefits and drawbacks. Depending on your architecture, there are versions that are faster than the libc version.
None of the functions you have listed are true equivalents, so, your situation will dictate which you need to use.
None of the functions you have listed are true equivalents, so, your situation will dictate which you need to use.
strcpy and stpcpy are functionally equivalent, but differ only in return value. stpcpy is superior because it points to the end of the string, thus letting the caller know how many bytes were copied.
strdup is nice, but it always depends on your use case.
memcpy is probably best if you're keeping track of lengths separately.
memcpy is probably best if you're keeping track of lengths separately.
strlcpy
2. strncpy
3. stpcpy
4. memcpy