const int *p与int *const p的区别
2015-09-23 11:00
435 查看
转载于:http://blog.csdn.net/suer0101/article/details/8032269
本文只是一篇学习笔记,是看了《彻底搞定C指针》中的相关篇幅后的一点总结,仅此而已!
一、先搞清const int *p与int const *p的区别
它们的区别就是:没有区别!!
无论谁在前面都没有影响!所以const int *p与int const *p用法一样!
二、const int *p的用法
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int test1 = 1;
int test2 = 2;
const int *p;
p = &test1;
p = &test2;
test2 = 3;
//*p = 4; error: assignment of read-only location ‘*p’
printf("%d\n", *p);
return 0;
}
执行结果 :3 ,这个好理解,如果加入被我注释掉的那一行就会报错,编译通不过,我用的是gcc version 4.4.3。也就是说*p是常量,不可更改,但指针p还是变量,你想怎么 变都可以。
三、int *const p的用法
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int test1 = 1;
int test2 = 2;
int *const p = &test1; //只能在声明的时候就给它赋初值,否则还是会报错的
//p = &test2; error: assignment of read-only location ‘*p’
test1 = 3;
printf("%d\n", *p);
return 0;
}
执行结果 :3 ,这样用p是常量,也就是说p所指向的地址是不可以更改的,所以当把test2的地址赋值给p时就会报错!但是p所指的地址内容是可以改变的。
三、补充const int *const p
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int test1 = 1;
int test2 = 2;
const int *const p = &test1;
//p = &test2;
//*p = 3;
printf("%d\n", *p);
return 0;
}
执行结果 :1,这个就相当于以上两种情况的混合体,p是常量,所以不能把test2的地址赋给p;同时*p也是常量,所以*p的内容不可更改!
本文只是一篇学习笔记,是看了《彻底搞定C指针》中的相关篇幅后的一点总结,仅此而已!
一、先搞清const int *p与int const *p的区别
它们的区别就是:没有区别!!
无论谁在前面都没有影响!所以const int *p与int const *p用法一样!
二、const int *p的用法
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int test1 = 1;
int test2 = 2;
const int *p;
p = &test1;
p = &test2;
test2 = 3;
//*p = 4; error: assignment of read-only location ‘*p’
printf("%d\n", *p);
return 0;
}
执行结果 :3 ,这个好理解,如果加入被我注释掉的那一行就会报错,编译通不过,我用的是gcc version 4.4.3。也就是说*p是常量,不可更改,但指针p还是变量,你想怎么 变都可以。
三、int *const p的用法
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int test1 = 1;
int test2 = 2;
int *const p = &test1; //只能在声明的时候就给它赋初值,否则还是会报错的
//p = &test2; error: assignment of read-only location ‘*p’
test1 = 3;
printf("%d\n", *p);
return 0;
}
执行结果 :3 ,这样用p是常量,也就是说p所指向的地址是不可以更改的,所以当把test2的地址赋值给p时就会报错!但是p所指的地址内容是可以改变的。
三、补充const int *const p
[cpp] view
plaincopy
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int test1 = 1;
int test2 = 2;
const int *const p = &test1;
//p = &test2;
//*p = 3;
printf("%d\n", *p);
return 0;
}
执行结果 :1,这个就相当于以上两种情况的混合体,p是常量,所以不能把test2的地址赋给p;同时*p也是常量,所以*p的内容不可更改!
相关文章推荐
- 程序员经典面试题之——数组指针与指针数组
- C++设计模式——组合模式
- iOS SDK详解之UIEvent/UITouch
- 百度歌曲接口
- ArcGIS教程:绘制等值线的工作原理
- 相似图片搜索的原理(二)
- Mysql grant权限详解
- ReactiveCocoa2实战
- LeetCode(42) Maximum Depth of Binary Tree
- C语言深度学习——入门篇
- iOS消息推送机制的实现
- C语言实验——某年某月的天数
- 查看ajax的错误信息
- iOS 使用UILocalizedIndexedCollation实现区域索引标题(Section Indexed Title)即拼音排序
- html图片预览
- vim乱码问题
- PHP面试总结
- Centos6.5安装与配置JDK-8(简单实用)
- 缺陷管理工具整理
- 2 Eclipse的衍生版本