我是一个初学者,现在我正在学习如何用C语言复制字符串。
这是我刚刚遇到的一个问题:
每次我尝试使用"strcpy“命令从字符串1复制到字符串2时,Visual Studio2013都会给我一个错误/警告消息,告诉我"strcpy”是不安全的,并建议我使用strcpy_s。
你能解释一下为什么使用strcpy是不安全的吗?还有什么是strcpy更安全的替代方案?
下面是我的代码:
代码语言:javascript运行复制#include
#include
main()
{
char str1[] = "Copy a string.";
char str2[15];
char str3[15];
int i;
/* with strcpy() */
strcpy(str2, str1);
/* Without strcpy() */
for (i = 0; str1[i]; i++)
str3[i] = str1[i];
str3[i] = '\0';
/* Display str2 and str3 */
printf("The content of str2: %s\n", str2);
printf("The content of str3: %s\n", str3);
return 0;
}附注:我在Windows 7 64位旗舰版上使用Visual Studio 2013 64位旗舰版。谢谢您抽时间见我!