指针常量和常量指针的区别

 我来答
hikehk
2011-12-28 · TA获得超过240个赞
知道小有建树答主
回答量:166
采纳率:100%
帮助的人:54万
展开全部
1 常量指针!如 int b, c; int * const a = &b;
表示a是一个常量指针它指向变量b的内存。但是因为是常量指针所以不能再用a指向其他变量,如 a = &c; 错误!可以修改指向内存的值,如:*a = 20; BTW 常量指针声明的时候必须向我那样赋初值。

2 指向常量的指针!如 int b, c; int const *a; a = &b; a = &c;
都可以,唯独它指向的内存不能被修改。如:*a=20;这是违法的!错误!

这就是主要区别!

BTW 还有一个记住他们不同形式的技巧!看const关键字,他后面的不可修改,如int * const a = &b; 后面是a,则说明a不能修改!
int const * a = &b;后面是*a则说明*a不可被修改!

在好多书上或MSDN是经常用 const int a=5;
int b=6;
const int *p=&b;
其实 const int* 和int const* 一样,就是常指针 也就是它所指向的数据(在这是int)是常量,它自己的数据类型是const int*
还有const int *p=&b;是可以的 虽然b不是常量。
但是 const int a=6;
int *p=&a;
会报错,因为它消除了a的const属性
**********************************************

*******我们可以总结一下********
1. 对于常量(符号常量)和常指针、常引用常对象声明都是一样的
定义格式: const 数据类型 常量名=常量值;
或 数据类型 const 常量名=常量值;

例如:const int a=7; 或 int const a=7;(符号常量)
int b=5;
const int *p=&b; 或 int const *p=&b;(常指针)
const int &m=b;(常引用)
const Point pt; 或 Point const pt; //常对象不能更新
常指针和常引用在功能上有所限制,就是不能通过他们更改其指向的变量的数据(值)

2. 指针常量
定义格式: 数据类型 *const 指针常量=常量值;
如char ch,*const pch=&ch;(我是一步完成的,你也可以分开)
也就是说这个指针本身是个常量,不可改变,即它所指向的地址是固定的。但,ch是可以改变的。

************************************

下面是MSDN中关也常对象的说法
///////////////////////////////////
Initializing Pointers to const Objects
A pointer to a const object can be initialized with a pointer to an object that is not const, but not vice versa. For example, the following initialization is legal:

Window StandardWindow;
const Window* pStandardWindow( &StandardWindow );

In the preceding code, the pointer pStandardWindow is declared as a pointer to a const object. Although StandardWindow is not declared as const, the declaration is acceptable because it does not allow an object not declared as const access to a const object. The reverse of this is as follows:

const Window StandardWindow;
Window* pStandardWindow( &StandardWindow );

The preceding code explicitly declares StandardWindow as a const object. Initializing the nonconstant pointer pStandardWindow with the address of StandardWindow generates an error because it allows access to the const object through the pointer. That is, it allows removal of the const attribute from the object.
///////////////////////////////////////

有问题我们再到HiKe电脑吧讨论!
来源:http://hike.bokee.com/6169539.html
longinus_dean
推荐于2018-05-06 · TA获得超过2282个赞
知道小有建树答主
回答量:1097
采纳率:50%
帮助的人:1502万
展开全部
int a = 10;
int * b = a;//指针
const int * c = a;//指向常量的指针
int * const d = a;//常量指针
const int * const * e = a;//常量指向常量的指针

你想问什么
b是可以修改自己,就是可以重新指向别人,也可以修改a的数据
c是不能修改a的数据,不过可以重新指向别人
d是不能重新指向别人不过可以修改a的数据
e是既不能指向别人也不能修改a的数据
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hyf372018331
2011-12-30 · TA获得超过128个赞
知道答主
回答量:12
采纳率:0%
帮助的人:9.3万
展开全部
指针常量:指针指向的内容是不允许修改。
常量指针:指针指向的内容是允许修改,指针指向的地址是不允许修改.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友5323d8ecac
2011-12-27
知道答主
回答量:48
采纳率:0%
帮助的人:15.8万
展开全部
常量指针就是指针只能指向常量的地址 常量是什么不用说了吧。。。int a=5;5的地址就是常量,a的是变量。p只能指向常量但是p可以改变,即可以指向不同常量。指针常量就是指针本身指向的地址不能变,即int *p=a; 这个p即&a不能变,但是a可以改变
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
laomaqing
2011-12-27
知道答主
回答量:18
采纳率:0%
帮助的人:14.4万
展开全部
指针常量是一个地址,常量指针是一个内容
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式