参见英文答案 >
Why is this string reversal C code causing a segmentation fault? 8个
#include <stdio.h> #include <stdlib.h> int main(void) { //char s[6] = {'h','e','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','\0'}; char *s = "hello";