[转]const指针与指向const的指针

时间:2023-03-09 03:50:14
[转]const指针与指向const的指针

经常忘记,保存一下..

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int a=;
int b; /*定义指向const的指针(指针指向的内容不能被修改)*/
const int* p1;
int const* p2; /*定义const指针(由于指针本身的值不能改变所以必须得初始化)*/
int* const p3=&a; /*指针本身和它指向的内容都是不能被改变的所以也得初始化*/
const int* const p4=&a;
int const* const p5=&b; p1=p2=&a; //正确
*p1=*p2=; //不正确(指针指向的内容不能被修改) *p3=; //正确
p3=p1; //不正确(指针本身的值不能改变) p4=p5;//不正确 (指针本身和它指向的内容都是不能被改变)
*p4=*p5=; //不正确(指针本身和它指向的内容都是不能被改变) return ;
}

引用地址: http://hi.baidu.com/study_cs/item/c4c469cb35bd890bac092f37