Can/Why using char * instead of const char * in return type cause crashes?

后端 未结 5 1628
醉梦人生
醉梦人生 2021-01-05 15:12

I read somewhere that if you want a C/C++ function to return a character array (as opposed to std::string), you must return const char* rather than char*. Doing the latter m

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-05 15:27

    Simply changing the return code won't cause a crash. However if the string you return is static (for example return "AString"), you should return const char * to make sure that the compiler detects any attempted modification of that memory, which will likely cause a crash. You can certainly use casts and such to get around the compiler checks, but in that case you'd have to work to make the crash happen.

提交回复
热议问题