先来看一下msdn上的函数原型和对其参数的解释
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
Parameters
strDestination
Location of the destination string buffer.
numberOfElements
Size of the destination string buffer in char units for narrow and multi-byte functions, and wchar_t units for wide functions.
strSource
Null-terminated source string buffer.
msdn上说第二个参数 numberOfElements 是目标缓冲区大小。
那么这个大小到底应该设置为多大?相信很多人都有同样的困惑,下面用代码测试一下几种情况。
int _tmain(int argc, _TCHAR* argv[])
{
char dest[10] = {0};
char src[] = "12345";