我正在尝试将指针传递给 strcpy() 函数。但是在运行这段代码时,我没有得到任何输出,并且程序停止响应,即使我没有得到任何编译错误。我知道 strcpy() 函数将参数作为指针,这里我直接传递指针。所以我想知道为什么下面的代码不能正常工作。还告诉我如何修改代码,如果可能的话使用相同的概念,即通过将指针传递给 strcpy() 函数
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
# include<string.h>
int main() {
char *rt = "dart";
char *ft = "gart";
strcpy(rt, ft);
printf("%s", rt);
system("PAUSE");
}
最佳答案
First of all you need a placeholder to store your to be copied source string using strcpy(). You need to declare a char array[] where you will copy the source string.
int main() {
char rt[10];
char *ft = "gart";
strcpy(rt, ft);
printf("%s", rt);
return 0;
}
Also make sure destination array 'rt' in this case has sufficient memory to store the to be copied string. Otherwise avoid using strcpy() and you should use strlcpy().
关于c - 将指针传递给 c 中的 strcpy 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41913326/