Chinaunix首页 | 论坛 | 博客
  • 博客访问: 51354
  • 博文数量: 16
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 195
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-05 14:28
文章分类

全部博文(16)

文章存档

2013年(16)

我的朋友

分类: C/C++

2013-10-05 15:50:05

关于const指针和指向const变量的指针很容易混淆

如:

1、指向非const变量的指针
int x=3,y;

const int *p=&x;

则p是指向const变量的指针,即使x不是const变量。

x=5//合法
p=&y//合法
*p=6//非法

2、指向const变量的指针

const int x=3,y;
const int *p=&x;

x=4//非法
p=&y;//合法
*p=6;//非法
3、指向非const变量的const指针

int x=3,y;
int * const p=&x;

x=4//合法
p=&y//非法
*p=6//合法,x=*p=6

4、指向const变量的const指针

const int x=3,y;
int * const p=&x;

x=4//非法
p=&y//非法
*p=6//合法

5、const int x=3,y

const int * const p=&x;

x=4//非法
p=&y//非法
*p=6//非法



阅读(844) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~