fra*_*rex 2 c string pointers segmentation-fault
这是一个初学者的问题:为什么这会打破/给出错误?
#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;
}
Run Code Online (Sandbox Code Playgroud)
cod*_*ict 11
更改
char *x="PM123BFD";
Run Code Online (Sandbox Code Playgroud)
至
char x[]="PM123BFD";
Run Code Online (Sandbox Code Playgroud)
您无法修改字符串文字,因此请将函数传递给char它可以修改的数组.
我不明白它为什么会破坏 - 我宁愿期待一个无限循环:while条件永远是真的,循环永远不会离开.
重做循环条件,它是borked.然后查看您拥有的变量:您永远不会更改任何一个p或的值len.这不对.此外,循环内部的代码比需要的复杂得多.想想你是否真的需要三个变量.
| 归档时间: |
|
| 查看次数: |
601 次 |
| 最近记录: |