1 char str[100]="changedStr"; 2 3 char *a = new char [100]; 4 strcpy(a,"String A"); 5 cout<<"a: "<<a<<endl; 6 *(char **)&a = str; 7 cout<<"a: "<<a<<endl; 8 9 char c[100]="String B"; 10 char * b = c; 11 cout<<"b: "<<b<<endl; 12 *(char **)&b = str; // 等价于 b = str 13 cout<<"b: "<<b<<endl; 14 15 cout<<"c: "<<c<<endl; 16 *(char **)&c = str; // 等价于 c = str. 前者未定义行为;后者编译器报错。 17 cout<<"c: "<<c<<endl;
结果:
a: String A
a: changedStr
b: String B
b: changedStr
c: String B
c: @Ti //未定义行为