gpt4 book ai didi

c - 使用指针的字符串连接

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:47 25 4
gpt4 key购买 nike

由于下面的代码给出了(段错误)错误(在运行时),我如何仅使用指针实现连接?

#include <stdio.h>
#include <string.h>
int main()
{
char *s1="Front";
char *s2="Back";
char *s3=strcat(s1,s2);
puts(s3);
return 0;
}

最佳答案

因为您正在尝试写入字符串文字。 char *s1 = "Front"; 行指向一个无法写入的字符串常量。

将其更改为 char s1[20] = "Front"; 它应该会像您预期的那样运行 - 只要您添加的字符不超过 14 个。

关于c - 使用指针的字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17634905/

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