top | item 3912445

Ask HN: Preferred string copy function in C?

1 points| codehero | 14 years ago

1. strcpy

2. strncpy

3. stpcpy

4. memcpy

4 comments

order
[+] cd34|14 years ago|reply
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.

[+] codehero|14 years ago|reply
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.
[+] steve-howard|14 years ago|reply
strdup is nice, but it always depends on your use case.

memcpy is probably best if you're keeping track of lengths separately.

[+] tlb|14 years ago|reply
strlcpy