C++ string literal data type storage

前端 未结 8 1080
一生所求
一生所求 2020-11-30 10:03
void f()
{
    char *c = \"Hello World!\"
}

Where is the string stored? What\'s the property of it? I just know it is a constant, what else? Can I

8条回答
  •  悲哀的现实
    2020-11-30 10:24

    It has static storage duration, so it exists throughout the life of the program. Exactly where the compiler/linker put initialized data varies. Returning a pointer to it from a function is fine, but be sure you return a char const * -- writing to the string causes undefined behavior.

提交回复
热议问题