我有以下简单的功能:
void reverse(char* str) {
if (str == NULL)
return;
char* end = str;
while(*end != NULL) {
end++;
}
end--;
while(str < end){
char temp = *str;
*str++ = *end;
*end-- = temp;
}
}
int main(int argc, char* argv[]) {
char* second = "SOMETHING\0";
cout << "Before Reverse String: " << second << '\n';
reverse(second);
cout << "Reverse String: " << second << '\n';
}
很简单,对吧?但是我在行中遇到了分段错误
*str++ = *end
*end-- = temp
我错过了什么?
谢谢!