为什么这个程序会给出分段错误? [英] Why does this program give segmentation fault?
本文介绍了为什么这个程序会给出分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个初学者的问题:为什么这会破坏/给出错误?
It's a beginners question: Why is this breaking/giving an error?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strtrim_right(char *p)
{
char *end;
int len;
len = strlen( p);
while (*p && len)
{
end = p + len-1;
if(isalpha(*end))
*end =0;
else
break;
}
return(p);
}
int main ()
{
char *x="PM123BFD";
strtrim_right(x);
printf("%s", x);
return 0;
}
推荐答案
改变
char *x="PM123BFD";
到
char x[]="PM123BFD";
你不能修改字符串文字,所以改为传递给函数一个 char
数组,它可以修改.
You cannot modify a string literal, so instead pass the function a char
array which it can modify.
这篇关于为什么这个程序会给出分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文