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

strncpy, when used as designed, can leave the target array without a terminating null character. Using that array later with any string function causes undefined behavior.

strcpy, when used as designed, always leaves the target array properly null-terminated. Using it properly is a bit more difficult; you have to make sure the target array is big enough to hold the source string.

This sequence:

    target[0] = '\0';
    strncat(target, source, sizeof target);
does what most people probably assume strncpy should do (assuming target is defined as an array rather than as a pointer).



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

Search: