常量指针与指针常量
常量指针
其实常量指针哈 指针就是地址 常量指针即地址存放的数据是个常量
把指针比作一个小房子 常量指针就是指定了小房子里住的人是(小红)
const int *p=5;\\定义了常量指针
我的理解是给地址和地址存放的数据建立了一对一的映射关系
那么问题来了 可以修改这个地址存放的数据吗?
(房子里入住登记了小红 小绿可以替换掉小红住到这个房间吗?)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ int x=5,y=4;
const int *p=&x;
*p=4;
system("PAUSE");
return 0;
}
报错了呀
指针常量的话
小绿是可以入住到 分配给小红的房间的
int * const p=&x; 定义一个指针常量
我们来试试吧
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ int x=5,y=4;
int * const p=&x;
*p=4;
system("PAUSE");
return 0;
}
运行一下 小绿成功入住小红的房间了
还没有评论,来说两句吧...