Why is my function returning garbage when it should return a char?

前端 未结 3 813
遥遥无期
遥遥无期 2021-01-14 17:55

I\'m a newbie in C++ learning the language and playing around. I wrote a piece of code which behavior I don\'t understand. Could someone explain why the code below prints ou

3条回答
  •  猫巷女王i
    2021-01-14 18:06

    It's because you return a pointer to a local variable, a local variable that goes out of scope when the function returns.

    You are already using std::string for the argument, use it instead of the array and the return pointer.

提交回复
热议问题