指向const对象的指针和const指针

[来源] 达内    [编辑] 达内   [时间]2012-09-08

指向const对象的指针和const指针

1.指向const对象的指针 
const char c='a'; 
const char c1='b'; 
const char* p=&c;

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">p=&c1;

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 上面的p就是指向const对象的指针,这里无法通过p来修改所指向对象的值,如果我们使用p来修改指向对象的值,编译器会报错。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">*p='c';//非法的修改,编译器不通过

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">但是我们可以修改p指针指向的对象,因为这里指针P不是const的。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 上面指针p是指向const对象的,假如指向一个非const对象,也无法通过p来修改指向对象的值,因为这里指针p是“自以为指向const的指针”。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">假如指针p指向非const对象,那么我们可以通过修改该对象的值来达到修改*p的值。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">char c='a';

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">const char* p=&c;

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">c='b';//这里是合法的,*p输出就是b 2.const指针

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">2.const指针

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">char c='a'; 
char* const p=&c;

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">这里常量指针p指向的是一个非常量对象c,我们可以*p='b';这样来修改c的值。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">指针p是常量指针,所以我们无法修改p指向其它对象,是否可以修改指针p指向对象的值取决于被指向对象是否是const。

< p style="margin: 5px auto; text-indent: 0px; color: rgb(0, 0, 0); font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">p=&c1;//这里就编译不过

资源下载