char * p_one = "this is my first char pointer";
char * p_two= "this is second";
strcpy(p_one ,p_two);
Run Code Online (Sandbox Code Playgroud)
考虑上面的代码.这会导致访问冲突错误.所以请帮助理解
"this is my first char pointer"字符串存储在内存中的哪个位置?堆或堆栈strcpy,即使它已经存储了第一个字符串.为什么"this is second"字符串不能复制到同一位置?strcpy那么"this is my first char pointer"p_one指向的字符串会发生什么?它留在记忆中吗?strcpy知道特定指针是否分配了内存?p_one,那么它将指向新分配的内存区域,字符串文字可能/可能不会留在内存中,但它保证在程序的整个生命周期内都是活动的.字符串文字具有静态持续时间生命周期.[参考2]好读:
[参考1]
char a [] =?string?有什么区别?和char*p =?string?;?
[参考文献2]
C中字符串文字的"生命周期"