Search code examples
cc-strings

The memory location of char array and string in c


Where are the string and char array stored?

int main ()
{
    int a = 0; //This should be stack
    char* p = "hello"; // why this is on the static?
    char k[10] = "hello"; //on the stack?
}

A textbook says that the char pointer (Char* a) will be stored on the static, from my understanding of "static memory", only these 2 will be stored on the static memory:

int a=0;// will on the static
int main()
{
    static xxxxx; //will on the static.
}


Solution

  • By 6.7.8.2, the string "hello" in char *p = "hello" is string literal.
    String literals are generally located in .rodata, to prevent modification. Also, global variable are located in .data section.