Just curious to know (as we use these functions often). I don’t see any practical difference between strncpy() and memcpy(). Isn’t it worth to say that effectively,
char* strncpy (char *dst, const char *src, size_t size)
{
return (char*)memcpy(dst, src, size);
}
Or am I missing any side effect? There is one similar earlier question, but couldn’t find an exact answer.
There is a difference, see this part of the
strncpypage you linked to (emphasis mine):So if the string to be copied is shorter than the limit,
strncpypads with zero whilememcpyreads beyond the limit (possibly invoking undefined behaviour).