Assuming this is a C question (in C++, you'd be using std::copy with arrays, and, come to think of it, you wouldn't be using arrays at all), the reason to prefer strncpy over memcpy with C strings is that memcpy expects to copy arrays of equal size, it literally copies x number of bytes, while strncpy can copy from a smaller C string to a larger destination array: it needs to know the size of the destination, it will figure out the size of the source on its own. It will also pad the destination with zeroes.
Oh, right, that's another reason: if you know the size of the source, std::copy/memcpy is more efficient than strncpy, since it doesn't have to look out for the zero.
Usually character arrays store some string literals (though it is not necessary). If a source array is larger than a destination array then the terminating zero will not be copied and you can not for example use the destination array in some output operatioms as
std::cout << destination_array;
because these operations assume that the array has the terminating zero. Or you can not apply such string functions as strlen, strcpy and others to such an array without the terminating zero.
If a source array is larger than a destination array then the terminating zero will not be copied and you can not for example use the destination array in some output operations as
std::cout << destination_array;
because these operations assume that the array has the terminating zero. Or you can not apply such string functions as strlen, strcpy and others to such an array without the terminating zero.
I understand your instructions in the case that (1) the destination array is
the same size as the source; (2) the destination array is larger than the
source; but I do not understand them in the case that
(3) the destination array is smaller than the source.
Yes. The result string will be empty because it can store only one symbol: terminating zero. If you want to copy more symbols then enlarge the destination array.