当前位置:网站首页>Mortal immortal cultivation pointer-1
Mortal immortal cultivation pointer-1
2022-07-06 13:27:00 【Programmer rock】
One 、 The goal of the tutorial
- Through the theme of mortal cultivation , Study C/C++ The pointer , From entry to mastery ( Mastery here refers to the mastery of knowledge points ).
- In the use of pointers , Meet the technical requirements of enterprise development and written examination .
- Effect demonstration : Make it easy FPS Unlimited ammunition and blood lock in shooting games 、 Blinking and other functions .
Two 、 Tutorial preparation
1. Prepare any development environment .gcc, g++, vs, devc++, vscode Wait for any one .
2. Don't know how to cultivate Immortals , You can also master this tutorial without obstacles .
3、 ... and 、 The Syllabus

Four 、 The influence of mortal cultivation on me
One man fixes the immortals , Especially later , There are many slots . But the flaws are not hidden , every last IT people , Can find their own shadow in it . Dark forest law of three bodies , There is no way to disprove , There is no evidence , It's too far away from us . And the jungle law of the jungle in the fairy world , But now you can see , Touches . Every self-study scholar , It's the casual practice without any support , Along the way , All kinds of pits and traps ...
Follow Rock, Let's practice together , To conquer completely C/C++ The pointer , Quickly improve your technical strength !
5、 ... and 、 Pointer practice period
5.1 Pointer zero foundation quick start
I'm going to the sea of stars , Look for Xiuxian cave , There is exuberant aura , It's just too far away from China , Thousands of miles apart , How can we “ teleport ”? The immortal world “ Transmission array ”, You deserve it !
The transmission array is in the cultivation world “ The pointer ”.

5.1.1 Use “ The pointer ” Make your own transmission array
char LuanXingHai; // A sea of stars , There is a lot of aura in the world of cultivating immortals , It is especially suitable for the continent of cultivating immortals .
// Let the sea of stars , Set to H, The way 1:
LuanXingHai = 'A';
printf("%c\n", LuanXingHai);
// Suppose we don't have permission directly to LuanXingHai Set up the data , You can use the method 2:
// 1. Record LuanXingHai The address of
int addr = &LuanXingHai;
// 2. Make a transmission array , Namely “ The pointer ”
char* p;
// 3. hold LuanXingHai The address of , Assign a value to the transmission matrix
p = addr; // Pay attention to C++ Language compiler , You need to cast
// 4. Use the transmission array ( The pointer ), Transmit data
*p = 'H';
printf("%c\n", LuanXingHai);Use pointer transfer matrix , Teleport to the holy land of immortality - A sea of stars .

5.1.2 Transmission array ( The pointer ) In two ways
// First usage : Read the data of the designated destination , That is to say “ read ”
char value = *p;
// Second usage , The data “ transmission ” To the designated destination , That is to say “ Write ”
*p = 'H';5.1.3 The relationship between pointer and address
Pointer value , It's essentially an address .
The pointer , It is equivalent to a transmission array . This transmission array , You need to go to an immortal or even a destination first , This destination , The destination transmission address is required , That's it “ The pointer ” Value .

5.1.4 The type of pointer
Different types of transmission arrays , The objects that can be transmitted are different , Force transmission , Talk lead “ Space cracks ”, Cause the object to be destroyed .
Different types of pointers , Only certain types of data can be transmitted , Force transmission , It may lead to unexpected consequences such as data damage .
int lingShi = 512; // Lingshi , A common currency in the celestial world , Equivalent to the gold of secular society
char * p = &lingShi; //C++ The compiler needs to cast
*p = 1;
printf("%d", lingShi); // In small end format computers , Output 513Particular attention , stay C++ In the compiler ( The suffix of the file is .cpp By default c++ compiler ), Different types of pointers are assigned directly , Fail to compile , A cast is required , But in a lot of C In the language compiler , You can assign values directly .
Pointer with mismatched type , Forced access to , May lead to unexpected consequences ! Similar to that in the immortal world “ Space cracks ”, Very dangerous ! The ancient Warcraft of the world of Warcraft , Is to invade the human world from the space crack , Lead to death ...

5.1.5 The underlying understanding of pointers

int lingShi = 100; // Lingshi , A common currency in the celestial world , Equivalent to the gold of secular society
int * p = &lingShi;
printf("%d", *p); // You need to access memory twice in a row 5.2 Addition and subtraction of pointers
Let's go shopping in the black market in the fairy world , Learn the addition and subtraction of pointers by the way .
Pointer plus integer
Stroll the black market , Ask about the price :
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
// Go to the black market and ask about the price of monsters
int* p = &prices[0]; //prices;
printf("%d\n", *p); //100
p = p + 1; // p++;
printf("%d\n", *p); // 200
p = p + 2;
printf("%d\n", *p); // 800Stroll around the black market in small steps , Ask about the price :
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
char* p2 = &prices[0]; //prices;
printf("%d\n", *p2); // 100
p2 = p2 + 1; // p++;
printf("%d\n", *p2); // 0Cause analysis :

Conclusion : The pointer +1, Point to the next of the pointer “ data ”. If it is char* The pointer , Then advance one byte , The value of the pointer is only added 1. If it is int* The pointer , Then point to the next integer , And an integer is occupied 4 Bytes of , So the value of the pointer , Actually added 4.
p = p + n; // The pointer p Value , Added n*sizeof(p The data type of the data pointed to )
Pointer minus integer
Step back and check the price
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int* p1 = &prices[1]; //prices;
printf("%d\n", *p1); //200
p1 = p1 - 1;
printf("%d\n", *p1); //100Step back and check the price
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
char* p2 = &prices[1]; //prices;
p2 = p2 - 1;
printf("%d\n", *p2); // 0Cause analysis :

p = p - n; // The pointer p Value , Less n*sizeof(p The data type of the data pointed to )
Pointer minus pointer
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
int* p1 = &prices[0];
int* p2 = &prices[5];
printf("%d\n", p2 - p1); // 5
char* p3 = &prices[0];
char* p4 = &prices[5];
printf("%d\n", p4 - p3); // 20Pointer minus pointer , The result is how many points are separated between these two pointers “ Corresponding to the type ” The data of .
Comparison of pointers
// Xiuxianjie black market ,1 Class to 8 The price of level monster
int prices[8] = { 100, 200, 500, 800, 1000, 2000, 5000, 10000 };
for (int *p1 = prices; p1 < prices + 8; p1++) {
printf("%d ", *p1); // Forward output various prices
}
printf("\n");
for (int* p1 = prices+7; p1 >= prices; p1--) {
printf("%d ", *p1); // Reverse output various prices
}Comparison of pointers , Is stored in the pointer “ Address values ”, Compare according to the rules of unsigned integers .
边栏推荐
- One article to get UDP and TCP high-frequency interview questions!
- 阿里云微服务(三)Sentinel开源流控熔断降级组件
- Data manipulation language (DML)
- Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
- 图书管理系统小练习
- Wei Pai: the product is applauded, but why is the sales volume still frustrated
- Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
- Cloud native trend in 2022
- View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
- Common method signatures and meanings of Iterable, collection and list
猜你喜欢

7. Relationship between array, pointer and array

What are the advantages of using SQL in Excel VBA

Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology

View UI plus released version 1.3.0, adding space and $imagepreview components

2-year experience summary, tell you how to do a good job in project management

12 excel charts and arrays

TYUT太原理工大学2022数据库题库选择题总结

C语言实现扫雷游戏(完整版)

Tyut Taiyuan University of technology 2022 introduction to software engineering summary

(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
随机推荐
A brief introduction to the database of tyut Taiyuan University of technology in previous years
string
系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
Inheritance and polymorphism (I)
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
Experience summary of autumn recruitment of state-owned enterprises
View UI plus released version 1.2.0 and added image, skeleton and typography components
最新坦克大战2022-全程开发笔记-3
学编程的八大电脑操作,总有一款你不会
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
【九阳神功】2019复旦大学应用统计真题+解析
4.30 dynamic memory allocation notes
2.初识C语言(2)
JS interview questions (I)
Arduino+ water level sensor +led display + buzzer alarm
【话题终结者】
初识指针笔记