Memory access violation. What's wrong with this seemingly simple program?

前端 未结 7 1582
清歌不尽
清歌不尽 2021-01-20 21:50

This is a quick program I just wrote up to see if I even remembered how to start a c++ program from scratch. It\'s just reversing a string (in place), and looks generally c

7条回答
  •  别那么骄傲
    2021-01-20 22:13

    You can't change string literals (staticly allocated). To do what you want, you need to use something like:

    int main()
    {
        char *str = new char[a_value];
        sprintf(str, "%s", );
        strReverse(str);
        delete [] str;
        return 0;
    }
    

    [edit] strdup also works, also strncpy... i'm sure there's a variety of other methods :)

提交回复
热议问题