当前位置:网站首页>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 .
边栏推荐
- 服务未正常关闭导致端口被占用
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- Novatel board oem617d configuration step record
- 4.30动态内存分配笔记
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- 初识C语言(下)
- Devops' future: six trends in 2022 and beyond
- [算法] 剑指offer2 golang 面试题4:只出现一次的数字
- Pride-pppar source code analysis
猜你喜欢
One article to get UDP and TCP high-frequency interview questions!
Code example of MATLAB reading GNSS observation value o file
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Matlab读取GNSS 观测值o文件代码示例
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
[algorithm] sword finger offer2 golang interview question 2: binary addition
FairyGUI条子家族(滚动条,滑动条,进度条)
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
[算法] 劍指offer2 golang 面試題2:二進制加法
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
随机推荐
基于rtklib源码进行片上移植的思路分享
Application architecture of large live broadcast platform
Iterable、Collection、List 的常见方法签名以及含义
Implementation of Excel import and export functions
Shortest Hamilton path (pressure DP)
Fgui project packaging and Publishing & importing unity & the way to display the UI
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
Detailed explanation of balanced binary tree is easy to understand
[算法] 劍指offer2 golang 面試題2:二進制加法
MySQL backup -- common errors in xtrabackup backup
雇佣收银员【差分约束】
Knowledge system of digital IT practitioners | software development methods -- agile
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Agile development helps me
音乐播放(Toggle && PlayerPrefs)
[Chongqing Guangdong education] Shandong University College Physics reference materials
分支语句和循环语句
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
Error: sorting and subscript out of bounds
What are the advantages of using SQL in Excel VBA