C语言指针使用技巧
来源:互联网 发布:金属材料牌号查询软件 编辑:程序博客网 时间:2024/05/17 18:15
1、指针在本质上也是一个变量;
2、指针需要占用一定的内存空间;
3、指针用于保存内存地址的值;
不同类型的指针占用的内存空间大小相同,可以通过以下例子看出:
- #include <stdio.h>
- int main()
- {
- int i;
- int* pI;
- char* pC;
- float* pF;
-
- pI = &i;
-
- printf("%d, %d, %0X\n", sizeof(int*), sizeof(pI), &pI);
- printf("%d, %d, %0X\n", sizeof(char*), sizeof(pC), &pC);
- printf("%d, %d, %0X\n", sizeof(float*), sizeof(pF), &pF);
-
- return 0;
- }
程序运行结果如下:
- lihacker@lihacker-laptop:/mnt/share/c$ ./a.out
- 4, 4 BF89351C
- 4, 4 BF893518
- 4, 4 BF893514
常量指针与变量指针
- const int* p; //p可变,p指向的内容不可变
- int const* p; //p可变,p指向的内容不可变
- int* const p; //p不可变,p指向的内容可变
- const int* const p;//p和p指向的内容都不可变
口诀:左数右指
当const出现在*号左边时,指针指向的数据为常量;
当const出现在*后右边时,指针本身为常量
- #include <stdio.h>
- int main()
- {
- int i = 0;
- const int* p = NULL;
- p = &i;
-
- *p = 10;
-
- return 0;
- }
编译时会报错:
- lihacker@lihacker-laptop:/mnt/share/c$ gcc test1.c
- test1.c: In function ‘main’:
- test1.c:10: error: assignment of read-only location ‘*p’
此时说明第10行语句:*p = 10 出错。
0 0
- C语言指针使用技巧
- 【C语言】指向指针的指针 char * *使用技巧
- c语言指针使用
- C语言使用技巧
- C语言 const 指针使用技巧之——返回指针的函数
- C语言多维数组指针的技巧
- C语言 指针的使用
- C语言指针使用陷阱
- C语言指针使用总结
- C语言指针使用小结
- C语言指针使用小结
- c语言指针基本使用
- C语言指针使用总结
- C语言--指针的使用
- C语言指针使用陷阱
- C语言指针的使用
- C语言 指针的使用
- c语言指针的指针使用
- BZOJ 1012, 最大数
- 重装系统遇上reboot and select proper boot device ...问题解决方案
- 快速排序的思想
- linux虚拟机无法启动问题研究
- 继承映射详解
- C语言指针使用技巧
- 二级列表ExpandableListView
- Java中关于内存泄漏出现的原因以及如何避免内存泄漏(超详细版汇总下)
- abstract的methods不能以private修饰(Java)
- Windows核心编程 第六章 线程基础知识 (上)
- 卡尔曼滤波(KF)与扩展卡尔曼滤波(EKF)的一种理解思路及相应推导(1)
- 微信分享闪退的解决办法
- 关联映射之多对一
- hdu5009(dp+双向链表优化)