当前位置:网站首页>C language basics -- pointers
C language basics -- pointers
2022-08-05 01:34:00 【 Childhood】
1. What is the pointer
In computer science, a pointer is an object in a programming language whose value points directly to a value stored elsewhere in computer memory, using an address.Since the desired variable unit can be found through the address, it can be said that the address points to the variable unit.Therefore, the visualization of the address is called "pointer", which means that it can find the memory unit with its address.
The pointer is a variable, the address (number) of the memory storage unit
A unit is 1 byte
Summary:
- The pointer is used to store the address, and the address is the only one that identifies a piece of address space
- The size of the pointer is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms
- 1 byte 8 bits
#include int main(){int a = 10;int *p = &a;return 0;} 2. Pointers and pointer types
#include int main(){printf("%d\n",sizeof(char*));printf("%d\n",sizeof(short*));printf("%d\n",sizeof(int*));printf("%d\n",sizeof(double*));return 0;}// Pointer type, on 64-bit platform, the output result is 8 The type of pointer determines the size of the space that can be accessed when the pointer is dereferenced
The pointer type determines: How far the pointer goes forward or backward
int *p; //*p can access 4 byteschar *p; //*p can access 1 bytedouble *p; //*p can access 8 bytesint *p+1; //*p skips 4 byteschar *p+1; //*p skips 1 bytedouble *p+1; //*p skips 8 bytesChange each element in the array to 1 through the pointer (important, worth reference)
#include int main(){int arr[10] = {0};int *p = arr; // array name -- address of first elementint i = 0;for(i = 0;i<10;i++){*(p+i) = 1; // replace each element in the array with 1}return 0;} Summary: The type of the pointer determines how much authority it has when dereferencing the pointer (can operate several bytes), for example: the pointer dereference of char* can only access one byte, while the dereference of int*Dereferencing the pointer gives access to 4 bytes.
3. Wild Pointer
int a; // Local variables are not initialized, the default is a random valueint *p; // local pointer variables are initialized to random valuesThe pointer variable is not initialized, the pointer is accessed out of bounds, and the space pointed to by the pointer is released (the address of the local variable is taken), and a wild pointer will be generated
3.1 How to avoid wild pointers
1. Pointer initialization
#include int main(){int b = 0;int a = 10;int *p = &a; // initializationint *p = NULL; // NULL -- used to initialize the pointer and assign a value to the pointerreturn 0;} 2. Be careful of pointer out of bounds
3. The pointer points to the space release, even if it is set to NULL
4. Check the validity of the pointer before use
4. Pointer operation
- Pointer+-Integer
- pointer-pointer
The number of intermediate elements is obtained
- Relational operations on pointers
Pointers can be compared in size
5. Pointers and arrays
The address of the first element of the array name
#include int main(){int arr[10] = {0};printf("%p\n", arr);printf("%p\n", &arr[0]);printf("%p\n", &arr);return 0;}// 1. &arr --&Array name, the array name is not the address of the first element, the array name represents the entire array// &Array name, the address of the entire array is taken out// 2. sizeof(arr) -- sizeof (array name) -- the entire array represented by the array name// --sizeof(array name) calculates the size of the entire array 6. Secondary pointer
#include int main(){int a = 10;int *pa = &a;int **ppa = &pa; // secondary pointerreturn 0;} 7. Pointer array
An array of pointers, essentially an array
#include int main(){int a = 10;int b = 20;int c = 30;int *arr[3] = {&a, &b, &c};int i = 0;for (i = 0; i < 3; i++){printf("%d\n", *(arr[i]));}return 0;}// Array of integers -- store integers// character array -- store characters// pointer array -- store pointers 边栏推荐
- GCC:编译时库路径和运行时库路径
- IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
- [How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?
- 迅睿cms网站搬迁换了服务器后网站不能正常显示
- 汇编语言之源程序
- LiveVideoStackCon 2022 上海站明日开幕!
- 一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
- 深度学习原理学习小结 - Self-Attention/Transformer
- 超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
- 原生js实现多选框全部选中和取消效果
猜你喜欢
![[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective](/img/de/944b31c68cc5b9ffa6a585530e7be9.png)
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective

数仓4.0(三)------数据仓库系统
![Binary tree [full solution] (C language)](/img/4d/2d81dc75433c23c5ba6b31453396f0.png)
Binary tree [full solution] (C language)

深度学习:使用nanodet训练自己制作的数据集并测试模型,通俗易懂,适合小白

Getting Started with Kubernetes Networking

source program in assembly language

Introduction to JVM class loading

5. PCIe official example

CNI (Container Network Plugin)

第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
随机推荐
内存取证系列1
EBS利用虚拟列及hint 提示优化sql案例一则
Software Testing Interview Questions: What do test cases usually include?
执掌图表
The difference between a process in user mode and kernel mode [exclusive analysis]
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective
GCC: compile-time library path and runtime library path
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
GCC:屏蔽动态库之间的依赖
linux(centOs7)部署mysql(8.0.20)数据库
5.PCIe官方示例
数仓4.0(三)------数据仓库系统
C语言基础知识 -- 指针
Opencv——视频跳帧处理
工具类总结
DDOS攻击真的是无解吗?不!
PCIe Core Configuration
Binary tree [full solution] (C language)
2021年11月网络规划设计师上午题知识点(上)
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征