Reverse a string in c [duplicate]
问题 This question already has answers here : In-Place String Reverse in C (3 answers) Closed 6 years ago . Im trying to reverse a string in place. void reverseStr(char *str) { int i; int length; int last_pos; length = strlen(str); last_pos = length-1; for(i = 0; i < length / 2; i++) { char tmp = str[i]; str[i] = str[last_pos - i]; str[last_pos - i] = tmp; } } Program received signal SIGSEGV, Segmentation fault. 0x0000000000400893 in reverseStr (str=0x400974 "Haus") at main.c:102 102 str[i] = str