gpt4 book ai didi

c - 段错误 - C

转载 作者:太空狗 更新时间:2023-10-29 16:46:12 25 4
gpt4 key购买 nike

<分区>

为什么下面的代码会返回一个段错误?当我注释掉第 7 行时,段错误消失了。

int main(void){
char *s;
int ln;
puts("Enter String");
// scanf("%s", s);
gets(s);
ln = strlen(s); // remove this line to end seg fault
char *dyn_s = (char*) malloc (strlen(s)+1); //strlen(s) is used here as well but doesn't change outcome
dyn_s = s;
dyn_s[strlen(s)] = '\0';
puts(dyn_s);
return 0;
}

干杯!

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