代码:
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 void fun(char **p){ 7 //char* s = "world"; 8 char s[] = "world"; 9 printf("point s = %p\n",s); 10 *p = s; 11 printf("point p = %p\n",*p); 12 } 13 14 int main(){ 15 16 char *p = "hello"; 17 printf("point p = %p\n",p); 18 cout<<"func start"<<endl; 19 fun(&p); 20 cout<<"func end"<<endl; 21 printf("point p = %p\n",p); 22 cout<<p<<endl; 23 24 return 0; 25 }
输出:
point p = 0x400acc func start point s = 0x7ffe10660100 point p = 0x7ffe10660100 func end point p = 0x7ffe10660100 `
若将代码第8行注释并替换成第7行注释内容,输出为
point p = 0x400ac2 func start point s = 0x400aa0 point p = 0x400aa0 func end point p = 0x400aa0 world
分析:
常量字符串存储在文字常量区,而字符数组存储在栈上。