来,我们直接看代码
#include<stdio.h>
#include<string.h>
int main()
{
char ch[] = "hello world";
char str1[100] = { 0 };
char str2[100] = { 0 };
strcpy(str1, ch);
strncpy(str2, ch, 2);
printf("%s\n", str1);
printf("%s\n", str2);
return 0;
}
我们看以上代码,该代码在VS2019用C语言打出来是报错的,它的报错原因是
错误 C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
错误 C4996 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
意思是此函数或变量可能不安全,需要用strcpy_s()函数和strncpy_s()函数来代替,但是,代替之后,会继续报错,它的报错原因:是形参与实参的类型不同和用于的类型太少
而用VS2019中的C++打出来之后呢,strcpy()和strncpy()函数一样报错原因是函数或变量可