当前位置:网站首页>Explain C language 11 in detail (C language series)
Explain C language 11 in detail (C language series)
2022-06-26 13:03:00 【Cream dimple* ٩ ( ˊωˋ*)و*】
Catalog
2. The pointer is out of bounds
3. The space pointed to by the pointer frees up
3. The pointer points to the space to release the immediate setting NULL
4. Check the validity of the pointer before using it
Preface :
This series will share some knowledge about pointer with you , Although the pointer may be difficult to learn compared with the previous knowledge , But don't be afraid to follow Xiaobian into the world of pointers !
The pointer :
What is a pointer :
Every space in the memory will have a number , The number is the address , An address is a pointer .( Number ---- Address ---- The pointer )
The size of the pointer :
Pointer in 32 On the bit platform is 4 Bytes , stay 64 On the bit platform is 8 Bytes .
The meaning of pointer type :
Pointers are also typed , So what is the meaning of these types ?
1. The pointer type determines the permission of pointer dereference .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int* pa;
char* pc;
float* pf;
printf("%d\n", sizeof(pa));
printf("%d\n", sizeof(pc));
printf("%d\n", sizeof(pf));
return 0;
}The screenshot of the code running is as follows :
2. The pointer type determines how far the pointer can go in one step ( step ).
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = arr;
char* pc = arr;
printf("%p\n", p);
printf("%p\n", p + 1);
printf("\n");
printf("%p\n", pc);
printf("%p\n", pc + 1);
return 0;
}The code screenshot is as follows :

Wild pointer :
Wild pointer means that the position pointed by the pointer is unknown .
The origin of wild pointer :
1. Pointer not initialized
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
// The code is not runnable
int main()
{
int* p;
*p = 20;// uninitialized
return 0;
}2. The pointer is out of bounds
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = arr;
int i = 0;
for (i = 0; i <= 10; i++)
{
*p = i;
p++;
}
return 0;
}The screenshot of the code running is as follows :
An out of bounds occurrence is shown as follows :

3. The space pointed to by the pointer frees up
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int* test()
{
int a = 10;
return &a;
}
int main()
{
int* p = test();
*p = 20;// When modifying the value a Space has been released
return 0;
}How to avoid wild pointer :
1. Pointer initialization
When you don't know why to initialize, initialize to NULL.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int* p = NULL;
return 0;
}Clearly know the initialization value .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 20;
int* p = &a;
*p = 10;
printf("%d\n", *p);
return 0;
}The code screenshot is as follows :

2. Watch out for the pointer
stay C Array out of bounds behavior is not checked in the .
3. The pointer points to the space to release the immediate setting NULL
4. Check the validity of the pointer before using it
Pointer arithmetic :
1. The pointer +- Integers
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
int* pend = arr + 9;
while (p <= pend)
{
printf("%d\n", *p);
p++;
}
return 0;
}The code screenshot is as follows :

2. The pointer - The pointer
The pointer - The pointer gets the number between two elements .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
printf("%d\n", &arr[9] - &arr[0]);
return 0;
}The code screenshot is as follows :

The premise of pointer subtraction is that two pointers point to the same space .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
// Implement a function to calculate the length of characters
int my_strlen(char* str)
{
char* start = str;
while (*str != '\0')
{
str++;
}
return str - start;
}
int main()
{
int len = my_strlen("abc");
printf("%d\n", len);
return 0;
}The screenshot of the code running is as follows :
3. The relational operation of pointers
The standard stipulates : Allows a pointer to an array element to be compared with a pointer to the memory location after the last element of the array , However, it is not allowed to compare with a pointer to the memory location before the first element .

Pointers and arrays :
The array name is the address of the first element of the array .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
printf("%p\n", arr);// Array name
printf("%p\n", &arr[0]);// The first address of the array
return 0;
}The code is shown in the following screenshot :

The secondary pointer :
See code for explanation , The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 10;
int* pa = &a;// Here pa Is the first level pointer
int** ppa = &pa;// Here ppa It's a secondary pointer
int*** pppa = &ppa;// Here pppa It's a three-level pointer
return 0;
}

Pointer array :
Arrays also have pointers , Let's briefly introduce the format of pointer array .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[10];
char ch[5];
int* paar[5];// Shaping array pointers
char* pch[5];// Character array pointer
return 0;
}Conclusion :
That's all for you to share ! If you want to see it C Language other series , Click on the link below ! I hope that's helpful , Students who want to learn remember to pay attention to Xiaobian and study together ! If there are any mistakes in the article, you are also welcome to point out the maze for Xiaobian in time ( I'd like to thank you guys first !)
边栏推荐
- 小白懒人专用-win10-win11一键安装版
- UVa11582 [快速幂]Colossal Fibonacci Numbers!
- KVM 显卡透传 —— 筑梦之路
- 倍福PLC通过MC_ReadParameter读取NC轴的配置参数
- Detailed explanation of C const: definition and use of C constant
- [BSidesCF 2019]Kookie 1
- 第01章_Linux下MySQL的安装与使用
- Basic principle and application routine of Beifu PLC rotary cutting
- Photoshop 2022 23.4.1增加了哪些功能?有知道的吗
- Software testing - concept
猜你喜欢

快手实时数仓保障体系研发实践

倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换

初识-软件测试

Power Designer - Custom Comment button

Record a phpcms9.6.3 vulnerability to use the getshell to the intranet domain control

【Spark】.scala文件在IDEA中几种图标的解释
![[esp32-C3][RT-THREAD] 基于ESP32C3运行RT-THREAD bsp最小系统](/img/4a/503240b332e3279047c438f1d9845e.png)
[esp32-C3][RT-THREAD] 基于ESP32C3运行RT-THREAD bsp最小系统

Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan

Software testing - concept

倍福PLC基于NT_Shutdown实现控制器自动关机重启
随机推荐
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
Accumulation of interview questions
小白懒人专用-win10-win11一键安装版
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
[极客大挑战 2019]RCE ME 1
详细讲解C语言11(C语言系列)
Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"
KVM video card transparent transmission -- the road of building a dream
[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3
Copy multiple Excel files and name them different
Electron official docs series: Processes in Electron
postgis計算角度
File remote synchronization and backup artifact Rsync
guacamole安装
map 取值
Less than 40 lines of code to create a blocprovider
心脏滴血漏洞(CVE-2014-0160)分析与防护
不到40行代码手撸一个BlocProvider
Electron official docs series: Contributing
Electron official docs series: Best Practices