当前位置:网站首页>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
边栏推荐
- what is wifi6?
- Box collapse and margin collapse
- Create process memory management copy_ Mm - processes and threads (IX)
- (11) Vscode code formatting configuration
- npm升级后问题,慌得一批
- Uniapp wechat sharing
- Laravel implements the Holy Grail model with template inheritance
- Qingdao agile tour, coming!
- [the background color changes after clicking a line]
- Escape characters \ splicing of data formats
猜你喜欢

2021-09-27

Laravel routing settings
![[untitled] Notepad content writing area](/img/0a/4a3636025c3e0441f45c99e3c67b67.png)
[untitled] Notepad content writing area

Three 之 three.js (webgl)旋转属性函数的简单整理,以及基于此实现绕轴旋转的简单案例

Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."

C#测试调用PaddleSharp模块识别图片文字

How to add beautiful code blocks in word | a very complete method to sort out and compare

Uncle wolf is looking for a translator -- Plato -- ongoing translation

狼叔来找翻译人员了--plato--持续翻译中.....

Kanban Quick Start Guide
随机推荐
The 2021 IT industry project management survey report was released!
Low code and no code considerations
Use pagoda + Xdebug + vscode to debug code remotely
es6数组的使用
egg的static的前缀是可以修改惹,靴靴
弘玑微课堂 | Cyclone RPA之“灵活的数字员工”执行器
(十一)vscode代码格式化配置
Study notes: unity customsrp-13-colorgrading
Use of ES6 array
Double click The jar package cannot run the solution
Project manager's thinking mode worth trying: project success equation
Create cells through JS (while loop)
JSP -- Introduction to JSP
可執行程序執行流程
Why can't V-IF and V-for be used together
Three methods of list rendering
Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
Anti crawler (0): are you still climbing naked with selenium? You're being watched! Crack webdriver anti crawler
[the background color changes after clicking a line]
Intel SGX preliminary learning and understanding notes (continuously updated)