当前位置:网站首页>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 .
边栏推荐
- Why can't d link DLL
- Android kotlin fragment technology point
- Android kotlin broadcast technology point
- 挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
- Engineers who can't read device manuals are not good cooks
- Let juicefs help you with "remote backup"
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
- 互联网常见34个术语解释
- Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
- D为何链接不了dll
猜你喜欢

(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software

Unity SKFramework框架(二十)、VFX Lab 特效库

Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking

TVOC, VOC, VOCs gas detection + Solution

Unity skframework framework (XX), VFX lab special effects library

2022零代码/低代码开发白皮书【伙伴云出品】附下载

PR usage skills, how to use PR to watermark?

最近公共祖先LCA的三种求法

Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
![[cloud native database] what to do when encountering slow SQL (Part 1)?](/img/ac/a59189aba1901e769beec1ec7e6d32.png)
[cloud native database] what to do when encountering slow SQL (Part 1)?
随机推荐
What are eNB, EPC and PGW?
OpenApi-Generator:简化RESTful API开发流程
Error function ERF
【笔耕不辍勋章活动】生命不止,写作不息
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
Pocket Raider comments
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
Daily question: 1175 Prime permutation
Unity skframework framework (XIII), question module
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
TVOC, VOC, VOCs gas detection + Solution
Daily practice of C language --- monkeys divide peaches
I did it with two lines of code. As a result, my sister had a more ingenious way
Mysql常用命令详细大全
Web Foundation
Unity SKFramework框架(十二)、Score 计分模块
Jerry's watch ringtone audition [article]
Essential for operation and maintenance - Elk log analysis system
OpenFOAM:lduMatrix&lduAddressing
Quantum three body problem: Landau fall

