const int *p与int *const p的区别
来源:互联网 发布:linux apache 框架 编辑:程序博客网 时间:2024/05/21 21:36
转自:http://blog.csdn.net/suer0101/article/details/8032269
本文只是一篇学习笔记,是看了《彻底搞定C指针》中的相关篇幅后的一点总结,仅此而已!
一、先搞清const int *p与int const *p的区别
它们的区别就是:没有区别!!
无论谁在前面都没有影响!所以const int *p与int const *p用法一样!
二、const int *p的用法
- #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的用法
- #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;
- }
三、补充const int *const p
- #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;
- }
0 0
- const int *p与int *const p的区别
- const int *p与int *const p的区别
- const int *p与int * const p的区别
- const int* p与int *const p的区别
- const int *p与int *const p的区别
- const int *p与int *const p的区别
- const int *p与int *const p的区别
- const int *p与int *const p的区别
- 关于const int *p与 int *const p的区别
- int * const p 与int const *p或者const int *p的区别
- const int *p,int *const p区别
- const int *p,int *const p区别
- const int *p,int *const p区别
- int * const p 与 const int *p
- int *const p 与 const int *p
- const int *p与int *const p
- int const *p与int *const p区别
- const指针,const int * p, int *const p,int const * p的区别
- Android release版本需要注意的问题
- 安卓如何判断APP是原生的还是WebView实现
- C--循环结构与结束语句
- docker本地graph存储以及 registry:v1 registry:v2(distribution)存储
- Oracle函数之LAG与LEAD
- const int *p与int *const p的区别
- Android SQLite数据库版本更新
- 第一次月赛 Little Pony and Sort by Shift
- 集合(关于collection)
- 封装Libsvm与Liblinear(重要概念之返回预测类别的概率分数)
- qtxlsx将excel中的数据读取到widget上
- 欢迎使用CSDN-markdown编辑器
- 创建Screen和MyRectangle类时遇到的问题总结
- 关于UFT的一些小想法