当前位置:网站首页>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 !)
边栏推荐
猜你喜欢

别乱用 FULL_CASE 和 PARALLEL_CASE

Echart堆叠柱状图:色块之间添加白色间距效果设置

不到40行代码手撸一个BlocProvider

Electron official docs series: Get Started

EasyGBS如何解决对讲功能使用异常?

The El form item contains two inputs. Verify the two inputs

MySQL 自定义函数时:This function has none of DETERMINISTIC, NO SQL 解决方案

P2393 yyy loves Maths II

计组实践实验9——使用CMStudio设计基于分段模型机微程序指令(2)
solo 博客系统的 rss 渲染失败
随机推荐
复制多个excel然后命名不同的名字
Vivado 错误代码 [DRC PDCN-2721] 解决
Goto statement to realize shutdown applet
ES6模块
倍福PLC基于CX5130实现数据的断电保持
倍福PLC基于NT_Shutdown实现控制器自动关机重启
Stream流学习记录
Software testing - Fundamentals
Redis learning - 03 transaction
P5733 【深基6.例1】自动修正
C# const详解:C#常量的定义和使用
微信小程序测试点总结
初识-软件测试
单例的常用创建和使用方式
倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试
National standard gb28181 protocol easygbs cascaded universal vision platform, how to deal with live message 403?
Adobe Acrobat阻止30款安全软件查看PDF文件 或存在安全风险
Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
自定义封装下拉组件