我是初学者,我正在学习如何在C中复制字符串.
这是我刚遇到的一个问题:
每次我尝试使用"strcpy"命令从字符串1复制到字符串2时,Visual Studio 2013会给我一条错误/警告消息,说"strcpy"不安全,建议我改用strcpy_s.
你能解释一下为什么使用strcpy不安全吗?什么是strcpy的更安全的替代品?
这是我的代码:
#include<stdio.h>
#include<string.h>
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;
}
Run Code Online (Sandbox Code Playgroud)
PS我在Windows 7 64位终极版上使用Visual Studio 2013 64位终极版.感谢您的时间!