当前位置:网站首页>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 .
边栏推荐
- Wechat applet development experience
- All in one 1405: sum and product of prime numbers
- 121 distributed interview questions and answers
- 国企秋招经验总结
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
- [algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
- 【GNSS】抗差估计(稳健估计)原理及程序实现
- Redis介绍与使用
- Liste des boucles de l'interface graphique de défaillance
- Record: newinstance() obsolete replacement method
猜你喜欢
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
[算法] 剑指offer2 golang 面试题1:整数除法
2-year experience summary, tell you how to do a good job in project management
Code example of MATLAB reading GNSS observation value o file
How do architects draw system architecture blueprints?
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
Novatel board oem617d configuration step record
Mixed use of fairygui button dynamics
Fundamentals of UD decomposition of KF UD decomposition [1]
rtklib单点定位spp使用抗差估计遇到的问题及解决
随机推荐
错误: 找不到符号
Record: solution of 404 error of servlet accessing database in dynamic web project
国企秋招经验总结
基于rtklib源码进行片上移植的思路分享
分支语句和循环语句
Record: Navicat premium can't connect to MySQL for the first time
堆排序【手写小根堆】
RTKLIB: demo5 b34f.1 vs b33
Rt-ppp test using rtknavi
系统设计学习(一)Design Pastebin.com (or Bit.ly)
Implementation of Excel import and export functions
[rtklib 2.4.3 B34] version update introduction I
PRIDE-PPPAR源码解析
初识C语言(下)
微信小程序开发心得
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
121道分布式面试题和答案
Music playback (toggle & playerprefs)
Detailed explanation of balanced binary tree is easy to understand
使用rtknavi进行RT-PPP测试