当前位置:网站首页>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 bytes
Change 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 values
The 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
边栏推荐
- 进程在用户态和内核态的区别[独家解析]
- DDOS攻击真的是无解吗?不!
- PCIe 核配置
- AI+PROTAC|dx/tx完成500万美元种子轮融资
- [Word] #() error occurs after Word formula is exported to PDF
- ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
- Day Fourteen & Postman
- 3. pcie.v 文件
- Xunrui cms website cannot be displayed normally after relocation and server change
- [Redis] Redis installation under Linux
猜你喜欢
详细全面的postman接口测试实战教程
How DHCP works
JWT简单介绍
tcp中的三次握手与四次挥手
数仓4.0(三)------数据仓库系统
If capturable=False, state_steps should not be CUDA tensors
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective
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?
自定义线程池
day14--postman interface test
随机推荐
深度学习训练前快速批量修改数据集中的图片名
第十一章 开关级建模
ExcelPatternTool: Excel表格-数据库互导工具
JVM类加载简介
A new technical director, who calls DDD a senior, is convinced
linux(centOs7)部署mysql(8.0.20)数据库
原生js实现多选框全部选中和取消效果
详细全面的postman接口测试实战教程
超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
Object.defineProperty实时监听数据变化并更新页面
如何发现一个有价值的 GameFi?
AI+小核酸药物|Eleven完成2200万美元种子轮融资
ORA-00257
Method Overriding and Object Class
day14--postman接口测试
Three handshake and four wave in tcp
AI+PROTAC|dx/tx完成500万美元种子轮融资
ORA-00257
ExcelPatternTool: Excel table-database mutual import tool