2

この単純な文字列反転関数を実装しようとしていますが、クラッシュし続けます。私はこれを 100 回実行しましたが、通常は char* の代わりに string を使用します。私は何が欠けていますか?

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;
}
4

1 に答える 1