gpt4 book ai didi

c - 为什么下面的代码会出现段错误问题?

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

<分区>

Possible Duplicate:
Why does simple C code receive segmentation fault?

我有下面的代码,可以从字符串中删除尾随空格,但我不知道进入这段代码,因为它会出现段错误问题??

 void main(void);

char* rtrim(char*);

void main(void)
{
char* trail_str = "This string has trailing spaces in it. ";


printf("Before calling rtrim(), trail_str is '%s'\n", trail_str);

printf("and has a length of %d.\n", strlen(trail_str));



rtrim(trail_str);



printf("After calling rtrim(), trail_str is '%s'\n", trail_str);

printf("and has a length of %d.\n", strlen(trail_str));

}


char* rtrim(char* str)
{
int n = strlen(str) - 1;

while (n>0)
{
if (*(str+n) != ' ')
{

*(str+n+1) = '\0';



break;
}

else

n--;
}

return str;

}

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