当前位置:网站首页>Introduction pointer notes
Introduction pointer notes
2022-07-06 13:07:00 【犇犇犇犇犇犇犇】
- What is the pointer
- Pointers and pointer types
- Wild pointer
- Pointer and pointer operation
- Pointers and arrays
- The secondary pointer
- Pointer array
What is the pointer ?
In the computer , All the data is stored in the memory , Different data types occupy different sizes of memory space . Memory is a contiguous address space in bytes , Each byte unit corresponds to a unique number , This number is called the address of the memory unit . such as :int Type account 4 Bytes ,char Type account 1 Bytes, etc . The system is in memory , The address of the first byte unit to allocate storage space for the variable , Call it the address of the variable . The address is used to identify each storage unit , It is convenient for users to correctly access the data in the storage unit . In high-level languages, addresses are vividly called pointers .
Pointers and pointer types
Here we introduce the code to explain
int main() {
printf("%d \n", sizeof(int*));
printf("%d \n", sizeof(char*));
printf("%d \n", sizeof(short*));
printf("%d \n", sizeof(double*));
return 0;
}
When we put this paragraph on the compiler and run it, we will find that it is the same size , Then why should we talk about pointer types . Look at the following code
int main() {
int a = 10;
int* pa = &a;
//*pa = 0;
char* pc = &a;
//*pc =0;
printf("%p\n", pa);
printf("%p\n", pc);
return 0;
}
You can see from the output that pa,pc At this time, the same address is stored, although pc yes char type , But the compiler will only report an alert that the type is incompatible or let pc Store a The address of . But if we change it by dereferencing a Stored value , There will be a different situation , You can watch it in the window memory of the compiler , When we use pa=0, here a All bytes become 0, When we use pc=0,a Only one byte in becomes 0.
The type of pointer determines , How much permission does the pointer have when dereferencing .
int* Able to control 4 Bytes
char* Able to control 1 Bytes
double* Able to control 8 Bytes
besides , Pointer types also work , Let's move on to the next code
int main() {
int arr[10] = {
0 };
int* pa = arr;
//char* pc = arr;
int i = 0;
for (i = 0; i < 10; i++) {
*(pa + i) = 1;
//*(pc + i) = 1;
}
return 0;
}
When we use the memory window to watch &arr The address of ,int Defined pa Control the change arr Value , Each element of the array becomes 1,char Defined pc Control change arr What about the value of , altogether 40 Bytes , Only the front 10 Bytes become 1. So the pointer type also determines how far the pointer can go in one step ( Refers to the step size of the pointer ).
int* p p+1 -->4;
char* p p+1–>1;
double* p p+1–>8;
Wild pointer
The position of the pointer is unknown ( Random , incorrect , There is no limit to )
- Pointer cross boundary access
- The pointer accesses the memory that has been released
- Pointer not initialized
int main() {
int a;// Local variable uninitialized
int* pa;// Local pointer variable uninitialized ,
*pa = 10;
return 0;
}
int main() {
int arr[10] = {
0 };
int* pa = arr;
int i = 0;
for (i = 0; i < 12; i++) {
pa++;
}
// The pointer is out of bounds
return 0;
}
int* test() {
int a = 10;
return &a;
}
int main() {
int* p = test();
*p = 0;
// The pointer has released space for access
return 0;
}
Remember to initialize pointer variables
Carefully control , Pointer out of range processing
After releasing space , Set... To the pointer NULL
Check the validity of the pointer before using it
The operation of the pointer
- The pointer -+ Integers
- The pointer - The pointer
- The relational operation of pointers
int main() {
int arr[10] = {
1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int* p = &arr[9];
for (i = 0; i < sz; i++) {
printf("%d ", *p);
p++;
}
//for (i = 0; i < 5; i++) {
// printf("%d ", *p);
// p-=2;
//}
return 0;
}
int main() {
int arr[5] = {
0,1,2,3,4 };
int* p = arr;
int* p2 = &arr[5];
int i = 0;
for (i = 0; i < p2 - p; i++) {
printf("%d ", i);
}
return 0;
}
int main() {
int arr[5] = {
0,1,2,3,4 };
int* p = arr;
int* p2 = &arr[5];
while (p2 > p) {
printf("%d ", *p);
p++;
}
return 0;
}
Pointers and arrays
The array name is the address of the first element
int main() {
int arr[10] = {
0 };
printf(" %p\n ", arr);
printf("%p\n ", arr+1);
printf("%p\n ", &arr[0]);
printf("%p\n ", &arr[0]+1);
printf("%p\n ", &arr);
printf("%p\n ", &arr+1);
return 0;
}
1.&arr -& Array name - Instead of taking the address of the first element, take the address of the entire array - The array name represents the entire array
2.sizeof(arr)-sizeof( Array name )- Is to calculate the size of the entire element - The array name represents the size of the entire array
int main() {
int arr[10] = {
0 };
int* pa = arr;
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
//for (i = 0; i < sz; i++) {
// *(pa + i) = i;
//}
//for (i = 0; i < sz; i++) {
// printf("%d ", arr[i]);
//}
for (i = 0; i < sz; i++) {
arr[i] = i;
}
for (i = 0; i < sz; i++) {
printf("%d ", *(pa + i));
}
return 0;
}
The secondary pointer
Pointer variables are also variables , If it's a variable, it has an address , Then the pointer to the pointer address is called the secondary pointer
int main() {
int a = 10;
int* pa = &a;
int** ppa = &pa;
int*** pppa = &ppa;
//...
printf("%d\n", **ppa);
printf("%d\n", a);
return 0;
}
The output is the same
Pointer array
A pointer array is an array that stores pointers
int main() {
int a = 10;
int b = 20;
int c = 30;
//int* pa = &a;
//int* pb = &b;
//int* pc = &c;
// Shape array - Storage shaping
// A character array - Store characters
// Pointer array - Store pointer
//int arr[3] = { 1,2,3 };
int* arr2[3] = {
&a,&b,&c };
int i = 0;
for (i = 0; i < 3; i++) {
printf("%d\n",*(arr2[i]));
}
return 0;
}
Thought and code must be combined , We must not just want not to practice , Use more code to realize understanding .
边栏推荐
- 错误:排序与角标越界
- Several high-frequency JVM interview questions
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
- Music playback (toggle & playerprefs)
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
- [algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
- 阿里云微服务(四) Service Mesh综述以及实例Istio
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
- Error: sorting and subscript out of bounds
- [rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
猜你喜欢
Answer to "software testing" exercise: Chapter 1
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
编辑距离(多源BFS)
2年经验总结,告诉你如何做好项目管理
MySQL shutdown is slow
Fgui project packaging and Publishing & importing unity & the way to display the UI
堆排序【手写小根堆】
FairyGUI循環列錶
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
《软件测试》习题答案:第一章
随机推荐
Interview Essentials: talk about the various implementations of distributed locks!
rtklib单点定位spp使用抗差估计遇到的问题及解决
基于rtklib源码进行片上移植的思路分享
Implementation of Excel import and export functions
Shortest Hamilton path (pressure DP)
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
FairyGUI循環列錶
2年经验总结,告诉你如何做好项目管理
String类
121 distributed interview questions and answers
Heap sort [handwritten small root heap]
Fairygui loop list
错误: 找不到符号
【无标题】
[algorithm] sword finger offer2 golang interview question 1: integer division
异常:IOException:Stream Closed
XV Function definition and call
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
最短Hamilton路径 (状压DP)
Excel导入,导出功能实现