【发布时间】:2020-06-15 01:32:10
【问题描述】:
字符串和字符数组存储在哪里?
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.
}
【问题讨论】:
-
我认为,当您使用字符串文字初始化固定大小的数组时,它实际上是一种方便的语法,类似于使用花括号语法初始化其他类型的固定数组。但在其他情况下,字符串文字存在于静态内存中,您可以拥有指向它们的指针。它们在程序的持续时间内持续,因此您不必担心释放它们。
-
能否请将名为
a的变量之一更改为其他变量int a和char a[10] -
没有
char* a。char a[10]是一个错误,因为a已经定义为int。