当前位置:网站首页>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 .
边栏推荐
- Unity skframework framework (XIX), POI points of interest / information points
- 运维必备——ELK日志分析系统
- PR usage skills, how to use PR to watermark?
- Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
- OpenFOAM:lduMatrix&lduAddressing
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
- 研究表明“气味相投”更易成为朋友
- Error function ERF
- TVOC, VOC, VOCs gas detection + Solution
- Record idea shortcut keys
猜你喜欢

Word efficiency guide - word's own template

2022 Heilongjiang provincial examination on the writing skills of Application Essays

Japan bet on national luck: Web3.0, anyway, is not the first time to fail!

Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises

2、 Frame mode MPLS operation

Fundamentals of face recognition (facenet)

题解:《你的飞碟在这儿》、《哥德巴赫猜想》

I did it with two lines of code. As a result, my sister had a more ingenious way

三翼鸟两周年:羽翼渐丰,腾飞指日可待

【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
随机推荐
Unity SKFramework框架(二十)、VFX Lab 特效库
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
Unity skframework framework (XX), VFX lab special effects library
Web基础
nohup命令
Lucky numbers in the [leetcode daily question] matrix
口袋奇兵点评
二、帧模式 MPLS 操作
Unity skframework framework (XIV), extension extension function
嵌入式软件开发
记忆函数的性能优化
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
Unity skframework framework (XV), singleton singleton
Daily practice of C language --- monkeys divide peaches
JS reverse massive creative signature
Achievements in science and Technology (27)
Engineers who can't read device manuals are not good cooks
Nohup command
Chinese name extraction (toy code - accurate head is too small, right to play)
研究表明“气味相投”更易成为朋友

