ANSI C 文字列の末尾をトリミングしようとしていますが、関数でセグメント フォールトが発生し続けtrim
ます。
#include <stdio.h>
#include <string.h>
void trim (char *s)
{
int i;
while (isspace (*s)) s++; // skip left side white spaces
for (i = strlen (s) - 1; (isspace (s[i])); i--) ; // skip right side white spaces
s[i + 1] = '\0';
printf ("%s\n", s);
}
int main(void) {
char *str = "Hello World ";
printf(trim(str));
}
理由が分からないようです。私は15の異なるtrim
機能を試しましたが、それらはすべてセグフォルトです。