当前位置:网站首页>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 .
边栏推荐
- nohup命令
- What are eNB, EPC and PGW?
- Astro learning notes
- 题解《子数整数》、《欢乐地跳》、《开灯》
- [cloud native database] what to do when encountering slow SQL (Part 1)?
- Clean up system cache and free memory under Linux
- 2022 Heilongjiang provincial examination on the writing skills of Application Essays
- De4000h storage installation configuration
- Mysql常用命令详细大全
- Word efficiency guide - word's own template
猜你喜欢

Solve "sub number integer", "jump happily", "turn on the light"

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner

Node.js通过ODBC访问PostgreSQL数据库

Redis数据库持久化

How to modify the error of easydss on demand service sharing time?

Why is the default of switch followed by break?

A better database client management tool than Navicat

Unity skframework framework (XII), score scoring module

无向图的桥
![2022 zero code / low code development white paper [produced by partner cloud] with download](/img/46/92c51090e0c476df3bcffd2d11fb6d.png)
2022 zero code / low code development white paper [produced by partner cloud] with download
随机推荐
Unity SKFramework框架(十二)、Score 计分模块
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Performance optimization of memory function
OpenFOAM:lduMatrix&lduAddressing
Astro learning notes
2022 Heilongjiang provincial examination on the writing skills of Application Essays
【云原生数据库】遇到慢SQL该怎么办(上)?
Unity skframework framework (XIII), question module
Daily question: 1175 Prime permutation
Research shows that "congenial" is more likely to become friends
Jerry's watch ringtone audition [article]
Explanation of 34 common terms on the Internet
题解《子数整数》、《欢乐地跳》、《开灯》
Find love for speed in F1 delta time Grand Prix
Pocket Raider comments
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
二、帧模式 MPLS 操作
文件的下载与图片的预览
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策

