c - 将指针传递给 c 中的 strcpy 函数

标签 c pointers

我正在尝试将指针传递给 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/

相关文章:

c - 为什么地址运算符 (&) 对于通过函数传递的数组很重要?

c - 在 C 中增加数组大小的奇怪方法?

在 C99 中收集没有浪费 malloc 的字符串

c++ - 填充指针 vector

正确使用结构体指针

c++ - 为什么在 QSharedPointer 中使用窗口时会发生段错误?

c - 如何在c中递增数组

c - 让 dyld 链接到可执行文件本身的函数?

c - 在 c 中将 0 打印为 000,即将十进制打印为 3 位

pointers - JNA 传递指针 ByReference