当前位置:网站首页>Pointer from entry to advanced (1)
Pointer from entry to advanced (1)
2022-07-02 13:38:00 【Xiao Mofan】
Catalog
Two 、 Pointers and pointer types
2.1 Pointers add and subtract integers
3.1 The cause of the wild pointer
2. Pointer cross boundary access
3. The space that the pointer points to is released
Four 、 The operation of the pointer
One 、 The concept of pointer
seeing the name of a thing one thinks of its function , The pointer can point to a certain position ; Pointer variable is the address used to store a certain number or a certain space ;
#include <stdio.h>
int main()
{
int a = 10;// Open up a space in memory
int* pa = &a;// Here, for variables a Go to the address , Use & The operator
/*a Variable occupancy 4 Bytes of space , Here is the general a Of 4 The address of the first byte of a byte is stored in p Variable
in ,p Is a pointer variable to */
printf("%d ", *pa);
return 0;
}
Two 、 Pointers and pointer types
Among the data types we have learned , There are integers 、 Short 、 floating-point 、 Long integer and so on ; So does the pointer have a type ? How pointers are defined ?
Pointer defined method :
type + * + Variable name
char* pc = NULL;// Character pointer --- The pointer variable is pc Its type is char*
int* pi = NULL;// Integer pointer --- The pointer variable is pi Its type is int*
short* ps = NULL;// Short integer pointer --- The pointer variable is ps Its type is short*
long* pl = NULL;// Long pointer --- The pointer variable is pl Its type is long*
float* pf = NULL;// Single precision floating point pointer --- The pointer variable is pf Its type is float*
double* pd = NULL;// Double precision floating point pointer --- The pointer variable is pd Its type is double*
2.1 Pointers add and subtract integers
Since the pointer stores the address of a certain number or a certain space , So what happens when the pointer adds or subtracts integers ?
#include <stdio.h>
int main()
{
int n = 10;
int* pi = &n;
//%p --- It prints the address in hexadecimal
printf("%p\n", &n);
printf("%p\n", pi);
printf("%p\n", pi + 1);
return 0;
}
2.2 Dereference of pointer
The pointer stores the address of a certain number or a certain space , When we want to get this number or the content of this space , You need to dereference ; for instance : You need to The key , Yes The key To open the door ; Login WeChat 、QQ、 Weibo needs Account and password , Yes Account and password To see the content ; These things we need can be understood as Dereference operation ;
#include <stdio.h>
int main()
{
int n = 10;
int* pi = &n;
printf("%d\n", *pi);
// Here it is pi Add one before '*' Sign means yes pi Dereference the stored address , Just to get the value corresponding to this address
return 0;
}
3、 ... and 、 Wild pointer
Concept : The position of the pointer is unknown ( Random 、 incorrect 、 There is no definite limit to )
3.1 The cause of the wild pointer
1. Pointer not initialized
We're creating local variable When , uninitialized , By default, this variable is a random value ; stay Local variable pointer in , It's the same ;
#include <stdio.h>
int main()
{
int *p;// Local variable pointer not initialized , The default value is random
*p = 20;
return 0;
}
2. Pointer cross boundary access
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = arr;
int i = 0;
for (i = 0; i <= 11; i++)
{
// When the pointer points to an array arr The scope of time ,p It's a wild pointer
*(p++) = i;
}
return 0;
}
3. The space that the pointer points to is released
Here you can briefly understand , Let's not go into details , I will make a supplement for you later ;
3.2 How to avoid wild pointer
1. The pointer needs to be initialized
2. Watch out for the pointer
3. Pointer to space release even if set to NULL
4. Avoid returning the address of a local variable
5. Check the validity of the pointer before using it
Here is the fourth point :
Look at the code below , Just touching the knowledge of pointer, I compiled such code , Thinking that the last print out is 10, The result is really 10 Do you ?
int* test() { int a = 10; int* p1 = &a; return p1; } int main() { int* p=test(); printf("%d\n", *p); return 0; }
Compile , The result is 10, I'm so happy ;
You think it's really 10 Do you ?
#include <stdio.h> int* test() { int a = 10; int* p1 = &a; return p1; } int main() { int* p=test(); printf("hehe\n"); printf("%d\n", *p); return 0; }
According to the code above , Just one more print in advance, hehe , In essence, it should not affect the final result ; But the results after debugging are as follows :
The result is 5, Here it is explained to avoid returning the address of local variables
Simply, the space will be covered ;
The specific reason :( See the creation and destruction of function stack frames )https://blog.csdn.net/sjsjnsjnn/article/details/122811828?spm=1001.2014.3001.5501
Four 、 The operation of the pointer
4.1 The pointer + - Integers
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));// The pointer p+ Subscript (0~9), The corresponding value is found through subscript access and dereference
}
return 0;
4.2 The pointer - The pointer
#include <stdio.h>
int my_strlen(char* s)
{
char* pc = s;
while (*pc != '\0')
{
pc++;
}// Here is to find '\0' The address of is subtracted from its first address ( The pointer - The pointer ) Get the number of elements
return pc - s;
}
int main()
{
char c[]="abcdef";
int ret = my_strlen(c);
printf("%d\n", ret);
return 0;
}
5、 ... and 、 Pointers and arrays
An array is a continuous space , If the address of the array is stored in the pointer variable , Combined with the above , What will change ?
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
int* p = arr;
printf("%p\n", arr);
printf("%p\n", &arr[0]);
printf("%p\n", p);
return 0;
}
From the results of operation , The address of the array name 、 The address and of the first element of the array p The deposit address is the same ;
Conclusion : When an array stores the address in a pointer variable, it stores the address of the first element of the array , It can also be said that p Deposit is 1 The address of ;
6、 ... and 、 summary
The above is the introduction of pointer , Help new contacts C The little friend of the language recognizes the initial face of the pointer , Update the advanced level of the pointer now , The content will be long , But it's rich .
边栏推荐
- PR usage skills, how to use PR to watermark?
- 能自动更新的万能周报模板,有手就会用!
- Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
- Unity skframework framework (XV), singleton singleton
- Fundamentals of face recognition (facenet)
- Chinese name extraction (toy code - accurate head is too small, right to play)
- Lucky numbers in the [leetcode daily question] matrix
- Nohup command
- [youcans' image processing learning course] general contents
猜你喜欢
Bridge of undirected graph
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
三翼鸟两周年:羽翼渐丰,腾飞指日可待
Finally, someone explained the supervised learning clearly
Unity skframework framework (XV), singleton singleton
I did it with two lines of code. As a result, my sister had a more ingenious way
题解《子数整数》、《欢乐地跳》、《开灯》
日本赌国运:Web3.0 ,反正也不是第一次失败了!
2022 zero code / low code development white paper [produced by partner cloud] with download
随机推荐
Engineers who can't read device manuals are not good cooks
Astro learning notes
【youcans 的图像处理学习课】总目录
互联网常见34个术语解释
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
Unity skframework framework (XXI), texture filter map resource filtering tool
OpenFOAM:lduMatrix&lduAddressing
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
Three methods of finding LCA of the nearest common ancestor
Unity SKFramework框架(十三)、Question 问题模块
Unity skframework framework (XIII), question module
What are eNB, EPC and PGW?
题解:《压缩技术》(原版、续集版)
EasyDSS点播服务分享时间出错如何修改?
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
能自动更新的万能周报模板,有手就会用!
每日一题:1175.质数排列
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP