Hacker News new | past | comments | ask | show | jobs | submit login

Why would you use strncpy on non-c-strings? Why not use memcpy?



Because strncpy's peculiarities means it acts as a conversion from a C-string to a null-padded fixed-length string, which interacts interestingly with %s: "%.*s" will stop at the first null or the provided length. If you use memcpy, you need to terminate your padded string by hand.

memcpy is more useful if your fixed-size strings are e.g. space-padded. Obviously it also works if you absolutely know your input is already a fixed-size string, but strncpy will work on both fixed-size and null-terminated inputs.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: