当前位置:网站首页>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 边栏推荐
猜你喜欢
![[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?](/img/eb/535ffaff9b535fbc73a4d56aab0b3a.png)
[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?

DHCP的工作过程

Memory Forensics Series 1

Getting Started with Kubernetes Networking
![Binary tree [full solution] (C language)](/img/4d/2d81dc75433c23c5ba6b31453396f0.png)
Binary tree [full solution] (C language)

Xunrui cms website cannot be displayed normally after relocation and server change
![[Machine Learning] 21-day Challenge Study Notes (2)](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Machine Learning] 21-day Challenge Study Notes (2)

LiveVideoStackCon 2022 上海站明日开幕!

pytorch的使用:卷积神经网络模块
![[GYCTF2020]EasyThinking](/img/40/973411c69d1e4766d22f6a4a7c7c01.png)
[GYCTF2020]EasyThinking
随机推荐
MySQL3
PCIe Core Configuration
B站7月榜单丨飞瓜数据B站UP主排行榜发布!
AI+小核酸药物|Eleven完成2200万美元种子轮融资
Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
10年测试经验,在35岁的生理年龄面前,一文不值
EL定时刷新页面中的皕杰报表实例
pytorch的使用:卷积神经网络模块
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
LiveVideoStackCon 2022 上海站明日开幕!
二叉树[全解](C语言)
行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
第十四天&postman
内存取证系列1
[Endnote] Word inserts a custom form of Endnote document format
【Word】Word公式导出PDF后出现井号括号#()错误
BC(转)[js]js计算两个时间相差天数
缺陷检测(图像处理部分)
Day Fourteen & Postman
pytorch的使用:使用神经网络进行气温预测