由于 ptr1 和 ptr 指向同一个地址,为什么取消引用 ptr1 并将其值设置为 20,而不是反映在 x 中?



由于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;
}

因为您的xconstint。修改const在C++中是未定义的。

最新更新