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

时间:2020-06-15 01:32:10

标签: c c-strings

字符串和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.
}

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

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