您的位置:首页 > 编程语言 > C语言/C++

修改常量指针所指向变量的方法

2016-03-28 23:51 357 查看
把一个非常量指针的地址赋给常量指针,然后修改非常量指针所指向的值。
#include<iostream>
using std::cout;
using std::endl;
int main( )
{
int abc=123;
int cbd=321;
const int* p1=&abc;
int* p2=&cbd;
p1=p2;
cout<<"the calue in non-const"<<*p2<<endl;
cout<<"value in const:"<<*p1<<endl;
(*p2)++;
//如果uncomment下行,就会编译问题。
//(*p1)++;
cout<<endl<<"value in non-const:"<<*p2<<endl;
cout<<"value in const:"<<*p1<<endl;
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言