gpt4 book ai didi

c - 在 char 指针上使用 strcpy 时程序中止? (在 char 数组上工作正常)

转载 作者:太空狗 更新时间:2023-10-29 16:50:53 28 4
gpt4 key购买 nike

我很困惑为什么以下内容不起作用:

char * f = "abcdef";
strcpy(f, "abcdef");
printf("%s",f);

char s[] = "ddd";
strcpy(&s[0], "eee");
printf("%s", s);

在这两个例子中,strcpy 都收到了一个 char *,但在第一个例子中,它死得很惨。

最佳答案

"abcdef""ddd" 是字符串文字,可能驻留在地址空间的只读部分。 char s[] = "ddd" 确保将此文字复制到堆栈 - 因此它是可修改的。

关于c - 在 char 指针上使用 strcpy 时程序中止? (在 char 数组上工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5645949/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com