#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