Sus*_* Kr 2 c memory string pointers memory-management
#include<stdio.h>
int main(void)
{
/* in this amessage is an array big enough to
* hold the sequence of character and '\0' */
char amessage[] = "now is the time";
/* in this case a character pointer is
* pointing to a string constant */
char *pmessage = "now is the time";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我很困惑如何在第二种情况下分配内存?是私有的内存副本....我的意思是作为第二个stmt写一些东西是安全的,并确保其他任何东西都不会覆盖相同的内存??
char *pmessage = "now is the time";
Run Code Online (Sandbox Code Playgroud)
创建一个字符串文字,字符串的内存分配在只读位置的某处,它是特定于编译器的实现依赖细节.
修改它将导致未定义的行为.因此,如果您使用字符串文字,则您有责任确保不要尝试写入此只读内存.