The memory location of char array and string in c

Viewed 477

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.
}

2 Answers

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.

a will be on the stack. p itself will be on the stack. However, the data to which it points will be in a read-only section of memory (not the stack).

Related