char数组和字符串在c中的存储位置

问题描述 投票:2回答:2

字符串和char数组存储在哪里?

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

一本教科书说char指针(Char * a)将存储在静态存储器上,据我对“静态存储器”的理解,只有这2个将存储在静态存储器中:

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

c c-strings
2个回答
4
投票

根据6.7.8.2"hello"中的字符串char *p = "hello"是字符串文字。字符串文字通常位于.rodata中,以防止修改。此外,全局变量位于.data部分。


1
投票

a将在堆栈中。 p本身将位于堆栈中。但是,它指向的数据将位于内存的只读部分(而不是堆栈)中。

© www.soinside.com 2019 - 2024. All rights reserved.