Scope of (string) literals

前端 未结 7 1275
半阙折子戏
半阙折子戏 2020-11-27 18:13

I always try to avoid to return string literals, because I fear they aren\'t defined outside of the function. But I\'m not sure if this is the case. Let\'s take, for example

7条回答
  •  攒了一身酷
    2020-11-27 19:05

    No, string literals do not have scope, so your code is guaranteed to work across all platforms and compilers. They are stored in your program's binary image, so you can always access them. However, trying to write to them (by casting away the const) will lead to undefined behavior.

提交回复
热议问题