const int *p 和int * const p 的区别

看例子:

int sloth = 3;

const int *p1 = &sloth;

int * p2 const = &sloth;

这样申明的话,不允许使用p1来修改sloth的值,但是p1可以指向其他的地址;

可以利用p2修改sloth的值,但是p2不允许指向其他地址。

 

第二个例子:

1、

int gorp = 16;

int chips = 12;

const int *p_snack = &gorp

*p_snack = 20;  (X)

p_snack = &chips;  (√)

注: *p_snack是const而p_snack不是const。

 

2、

int gorp = 16;

int chips = 12;

int * const p_snack = &gorp;

*p_snack = 20;  (√)

p_snack  = &chips;  (X)

注: p_snack是const而*p_snack不是const。

 

3、

int gorp = 16;

int chips = 12;

const int * const p_snack = &gorp;

*p_snack = 20;  (X)

p_snack  = &chips;  (X)

注: p_snack和*p_snack都是const。

posted @ 2014-08-13 14:57  蝴蝶狼  阅读(1015)  评论(0编辑  收藏  举报