由于ptr1和ptr指向同一地址,为什么要取消引用ptr1并设置其值设置为20,不反映在x.中
#include <iostream>
using namespace std;
int main()
{
const int x = 10;
const int *ptr = &x;
int* ptr1=(int*)ptr;
cout<<ptr<<endl<<ptr1<<endl;
*ptr1=20;
cout<<x;
}
因为您的x
是const
int。修改const
在C++中是未定义的。