visual studio 2019 strncpy_s的赋值和strncpy不同
来源:5-10 字符串基本操作2

mahsiaoko
2020-02-18
#include
#include
const int MAX_LEN_NUM = 16;
int main()
{
using namespace std;
char strHelloWorld1[] = "hello";
char strHelloWorld2[] = "world1";
char strHelloWorld5[MAX_LEN_NUM] = { 0 };
strcpy_s(strHelloWorld5, MAX_LEN_NUM, strHelloWorld1);
strncpy_s(strHelloWorld5, MAX_LEN_NUM, strHelloWorld2, 2);
strcat_s(strHelloWorld5, MAX_LEN_NUM, strHelloWorld2);
unsigned int len = strlen(strHelloWorld5);
for (unsigned int index = 0; index < len; ++index) {
cout << strHelloWorld5[index] << " ";
}
cout << endl;
return 0;
}
strncpy_s(strHelloWorld5, MAX_LEN_NUM, strHelloWorld2, 2);运行之后,
strHelloWorld5="wo",而不是“wollo”;
还有,设置了_CRT_SECURE_NO_WARNINGS之后,还是不能使用strcat等,依旧报错。
1回答
-
这个和strncpy的填充方式有关,你可以选中strncpy,点击F1,然后看MSDN的文档说明。
vs2019添加这个宏的话报什么编译错误呢042020-02-18
相似问题