首页 > 解决方案 > c中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.
}

标签: cc-strings

解决方案


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


推荐阅读