gpt4 book ai didi

c - 非常简单的C代码不起作用

转载 作者:行者123 更新时间:2023-11-30 21:24:35 25 4
gpt4 key购买 nike

#include <stdio.h>

char *strcat_ (char s1[], char s2[]) {
int x = 0, y = 0;
while (s1[x] != '\0') {
x++;
}
while (s2[y] != 0) {
s1[x] = s2 [y];
x++;
y++;
}
s1[x] = '\0';
return s1;
}

main() {
char c;
c = *strcat_("John ", "Trump");
printf ("%s", &c);
}

这就是我的代码,当我尝试运行时,我收到“总线错误:10”。

我对此很陌生,所以请记住这一点。

感谢您的帮助。

最佳答案

这些行中存在一些问题 -

char c;
c = *strcat_("John ", "Trump");
printf ("%s", &c);

1. 你的函数 return char * 而不是 char

2.调用函数时不要对其应用*运算符。

3.您倾向于修改函数中的常量,这会导致UB以及没有足够的内存来容纳连接部分

    c = *strcat_("John ", "Trump");
^^^^ This is a constant.

4.printf 中,如果要打印内容,请不要传递变量的地址。

可以这样写 -

char a[100]="John";            
//char c;
strcat_(a, "Trump") //let type be void and let char a[] hold complete string

关于c - 非常简单的C代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35816517/

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