gpt4 book ai didi

c - wcsncpy 和 wcscpy_s 有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 15:46:02 30 4
gpt4 key购买 nike

使用 wcscpy_s 和使用 wcsncpy 之间有什么实际区别吗?唯一的区别似乎是参数和返回值的顺序:

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 );

如果没有实际差异,为什么 Microsoft 需要将 wcscpy_s 添加到 Visual Studio,而 wcsncpy 已经可用并且是标准函数?

从 Visual Studio 移植到 gcc 时,是否可以将 wcscpy_s 替换为 wcsncpy

最佳答案

这两个函数的行为不同。

来自 the MSDN documentation of wcscpy_s :

Upon successful execution, the destination string will always be null terminated.

来自 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

和脚注 (#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.

请注意,strncpywcsncpy 并非设计用于以 null 结尾的字符串。它们专为与空填充的固定宽度字符串一起使用而设计。

关于c - wcsncpy 和 wcscpy_s 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14892037/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com