当前位置:网站首页>Several examples of pointer transfer, parameter transfer, value transfer, etc
Several examples of pointer transfer, parameter transfer, value transfer, etc
2022-04-23 05:27:00 【What’smean】
Code :
#include<iostream>
using namespace std;
// The first function uses pointers in the form of value passing , All the changes are limited to the inside of the function ,
// When the function is executed, the value of the pointer itself will not be changed , It doesn't change what the pointer is pointing to .
void SwapPoint1(int *p,int *q){
int * tmp = p;
p = q;
q = tmp;
}
// The second function also uses pointers in the form of value passing ,
// But inside the function, the memory is accessed directly by dereference and the content indicated by the pointer is modified .
void SwapPoint2(int *p,int *q){
int tmp = *p;
*p = *q;
*q = tmp;
}
// The parameter form of the function is int *&,
// Its meaning is , This parameter is a reference , The referenced object is a... In memory int The pointer ,
// In this way, you can treat the pointer as an object , Exchange the value of the pointer itself . It should be noted that ,
// Now that the last function exchanges pointers , Of course, the result of dereferencing the pointer will change accordingly .
void SwapPoint3(int *&p,int *&q){
int * tmp = p;
p = q;
q = tmp;
}
int main(){
int a=5,b=10;
int *p = &a, *q = &b;
cout<<" Exchange before p The value of is :"<< p<<"\t"<<"q The value of is :"<<q<<endl;
cout<<" Exchange before p The value is :"<<*p<<"\t"<<"q The value is :"<<*q<<endl;
cout<<endl;
SwapPoint1(p,q);
cout<<" After exchanging p The value of is :"<< p<<"\t"<<"q The value of is :"<<q<<endl;
cout<<" After exchanging p The value is :"<<*p<<"\t"<<"q The value is :"<<*q<<endl;
cout<<endl;
a = 5,b = 10;
p = &a,q = &b;
cout<<" Exchange before p The value of is :"<< p<<"\t"<<"q The value of is :"<<q<<endl;
cout<<" Exchange before p The value is :"<<*p<<"\t"<<"q The value is :"<<*q<<endl;
cout<<endl;
SwapPoint2(p,q);
cout<<" After exchanging p The value of is :"<< p<<"\t"<<"q The value of is :"<<q<<endl;
cout<<" After exchanging p The value is :"<<*p<<"\t"<<"q The value is :"<<*q<<endl;
cout<<endl;
a = 5,b = 10;
p = &a,q = &b;
cout<<" Exchange before p The value of is :"<< p<<"\t"<<"q The value of is :"<<q<<endl;
cout<<" Exchange before p The value is :"<<*p<<"\t"<<"q The value is :"<<*q<<endl;
cout<<endl;
SwapPoint3(p,q);
cout<<" After exchanging p The value of is :"<< p<<"\t"<<"q The value of is :"<<q<<endl;
cout<<" After exchanging p The value is :"<<*p<<"\t"<<"q The value is :"<<*q<<endl;
return 0;
}
Output :
Exchange before p The value of is :0x66ff24 q The value of is :0x66ff20
Exchange before p The value is :5 q The value is :10
After exchanging p The value of is :0x66ff24 q The value of is :0x66ff20
After exchanging p The value is :5 q The value is :10
Exchange before p The value of is :0x66ff24 q The value of is :0x66ff20
Exchange before p The value is :5 q The value is :10
After exchanging p The value of is :0x66ff24 q The value of is :0x66ff20
After exchanging p The value is :10 q The value is :5
Exchange before p The value of is :0x66ff24 q The value of is :0x66ff20
Exchange before p The value is :5 q The value is :10
After exchanging p The value of is :0x66ff20 q The value of is :0x66ff24
After exchanging p The value is :10 q The value is :5
Please press any key to continue . . .
版权声明
本文为[What’smean]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220544124179.html
边栏推荐
- C test calls the paddlesharp module to recognize pictures and words
- What are the most popular recruitment technical skills in 2022? You can't think of it
- egg的static的前缀是可以修改惹,靴靴
- Call the interface to get the time
- Laravel implements the Holy Grail model with template inheritance
- 日志简介和构建web应用
- How to set the initial value of El input number to null
- 世界与个人发展
- Study notes: unity customsrp-11-post processing --- bloom
- Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”
猜你喜欢
Arithmetic and logical operations
Source code analysis of how to use jump table in redis
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
2021-10-08
JS array common methods
What role do tools play in digital transformation?
Laravel routing settings
Laravel routing job
创建进程内存管理copy_mm - 进程与线程(九)
Good test data management, in the end how to do?
随机推荐
Tslint annotations ignore errors and restful understanding
相机成像+单应性变换+相机标定+立体校正
phphphphphphphp
可執行程序執行流程
Camera imaging + homography transformation + camera calibration + stereo correction
What are the reasons for the failure of digital transformation?
[no title] Click the classification jump page to display the details
Anti crawler (0): are you still climbing naked with selenium? You're being watched! Crack webdriver anti crawler
领域驱动模型DDD(三)——使用Saga管理事务
The 2021 IT industry project management survey report was released!
Various situations of data / component binding
IPI interrupt
Wbpack configuring production development environment
Study notes: unity customsrp-12-hdr
To understand Devops, you must read these ten books!
d.ts---更详细的知识还是需要看官网的介绍(声明文件章节)
The introduction of lean management needs to achieve these nine points in advance
String class understanding - final is immutable
MySQL series - install MySQL 5.6.27 on Linux and solve common problems
Basic use of sqlyog