这是 linux 内核用来去除字符串前导和尾随空格的代码:
char *strstrip(char *s)
{
size_t size;
char *end;
size = strlen(s);
if (!size)
return s;
end = s + size - 1;
while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';
while (*s && isspace(*s))
s++;
return s;
}
在这里,我这样使用它:
int main( void ){
/* strip test*/
char buffer2[60];
char* testy2 = buffer2;
testy2 = " THING! ";
printf("The stripped string: \"%s\"\n",strstrip(testy2));
return 0;
}
该程序编译正常,但执行时它指出:
Segmentation fault (core dumped)
为什么会这样?