当前位置:网站首页>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 .
边栏推荐
- Solve "sub number integer", "jump happily", "turn on the light"
- 机器学习基础(二)——训练集和测试集的划分
- Android kotlin fragment technology point
- 【OpenGL】笔记二十九、高级光照(镜面高光)
- MAC (MacOS Monterey 12.2 M1) personal use PHP development
- 2022 Heilongjiang provincial examination on the writing skills of Application Essays
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- Word efficiency guide - word's own template
- de4000h存储安装配置
- ADB basic commands
猜你喜欢
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
OpenFOAM:lduMatrix&lduAddressing
net share
Unity SKFramework框架(十三)、Question 问题模块
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
Bridge of undirected graph
Solve "sub number integer", "jump happily", "turn on the light"
Don't spend money, spend an hour to build your own blog website
Unity SKFramework框架(十五)、Singleton 单例
随机推荐
2022 Heilongjiang provincial examination on the writing skills of Application Essays
2022零代码/低代码开发白皮书【伙伴云出品】附下载
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
Everyone wants to eat a broken buffet. It's almost cold
Android kotlin fragment technology point
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
JS generates 4-digit verification code
Jerry's weather code table [chapter]
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
每日一题:1175.质数排列
Web基础
Jerry's watch modifies the alarm clock [chapter]
Word efficiency guide - word's own template
Jerry's watch ringtone audition [article]
题解:《压缩技术》(原版、续集版)
[OpenGL] notes 29. Advanced lighting (specular highlights)
验证失败,请检查您的回电网址。您可以按照指导进行操作
A better database client management tool than Navicat
2022 zero code / low code development white paper [produced by partner cloud] with download