我正在尝试实现这个简单的字符串反转功能,但它一直在崩溃。我已经这样做了一百次,但我通常使用字符串而不是 char*。我错过了什么?
void reverse(char* str)
{
//First determine the size of the string
int length = 0;
char* temp = str;
while(*temp)
{
temp++;
length++;
}
int start = 0;
int end = length - 1;
while(start < end)
{
char temp = str[start];
str[start] = str[end]; // I get a EXEC_BAD_ACCESS here for start = 0
str[end] = temp;
start++; end--;
}
cout<<"Reversed: "<<string(str)<<endl;
}