#include <stdio.h>
#include <stdlib.h>
int main(void)
{
//char s[6] = {'h','e','l','l','o','\0'};
char *s = "hello";
int i=0,m;
char temp;
int n = strlen(s);
//s[n] = '\0';
while (i<(n/2))
{
temp = *(s+i); //uses the null character as the temporary storage.
*(s+i) = *(s+n-i-1);
*(s+n-i-1) = temp;
i++;
}
printf("rev string = %s\n",s);
system("PAUSE");
return 0;
}
在編譯上,錯誤是分段錯誤(訪問衝突)。請告訴是什麼這兩個定義之間的差別:反轉字符串文字的分段錯誤
char s[6] = {'h','e','l','l','o','\0'};
char *s = "hello";
也許是一個不同的標題?雖然這個例子是反轉字符串的代碼,但實際的問題是關於修改數組和字符串文字 – akf 2010-07-03 16:54:09
你有沒有理由不使用'strrev()'?此外,這將打破多字節字符。 – Piskvor 2010-07-03 17:19:51