What is the difference between wcsncpy and wcscpy_s?
c, visual-c++
Solution
These two functions do not have the same behavior.
From the MSDN documentation of `wcscpy_s`:
Upon successful execution, the destination string will always be null terminated.
From the specification of `wcsncpy` (C11 7.29.4.2.2/1-3):
#include <wchar.h>
wchar_t *wcsncpy(wchar_t * restrict s1,
const wchar_t * restrict s2,
size_t n);
The `wcsncpy` function copies not more than `n` wide characters (those that follow a null wide character are not copied) from the array pointed to by `s2` to the array pointed to by `s1`.
If the array pointed to by `s2` is a wide string that is shorter than `n` wide characters, null wide characters are appended to the copy in the array pointed to by `s1`, until `n` wide characters in all have been written
and the footnote (#346):
Thus, if there is no null wide character in the first `n` wide characters of the array pointed to by `s2`, the result will not be null-terminated.
Note that `strncpy` and `wcsncpy` are not designed for use with null-terminated strings. They are designed for use with null-padded, fixed-width strings.
Problem
Is there any practical difference between using `wcscpy_s` and using `wcsncpy`? The only difference seems to be the order of parameters and return value: ``` errno_t wcscpy_s(wchar_t *strDestination, size_t numberOfElements, const wchar_t *strSource); wchar_t *wcsncpy(wchar_t *strDest, const wchar_t *strSource, size_t count ); ``` And if there is no practical difference, why did Microsoft need to add `wcscpy_s` to Visual Studio, when `wcsncpy` was already available and a standard function? Is it OK to replace `wcscpy_s` to `wcsncpy` when porting from Visual Studio to gcc?