我试图将字符串反转到位。
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[last_pos - i];
(gdb) print i
$1 = 0
(gdb) print last_pos
$2 = 3
(gdb) print str
$3 = 0x400974 "Haus"
(gdb) print str[3]
$4 = 115 's'
感谢您的阅读。我不明白,这个指令怎么会导致错误?问候 :)