当前位置:网站首页>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
边栏推荐
- selenium预先加载cookie的必要性
- App Store年交易额100万美元只缴15%佣金,中小开发者心里很矛盾
- Implementation of resnet-34 CNN with kears
- 使用宝塔+xdebug+vscode远程调试代码
- Camera imaging + homography transformation + camera calibration + stereo correction
- 可执行程序执行流程
- JS time format conversion
- Three of three JS (webgl) is simple to draw lines / arcs according to points (based on linegeometry / line2 / linematerial, draw two arc segments based on the center of the circle)
- Why can't V-IF and V-for be used together
- Log introduction and building web application
猜你喜欢

Open source rule engine - Ice: dedicated to solving flexible and complex hard coding problems

Kanban Quick Start Guide

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

Source code analysis of how to use jump table in redis

(十一)vscode代码格式化配置

Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行

Quick app bottom navigation bar

Camera imaging + homography transformation + camera calibration + stereo correction

Basic use of sqlyog

Five key technologies to improve the devsecops framework
随机推荐
egg的static的前缀是可以修改惹,靴靴
Membarrier (personal learning and understanding)
Redis的基本知识
Three 之 three.js (webgl)旋转属性函数的简单整理,以及基于此实现绕轴旋转的简单案例
弘玑微课堂 | Cyclone RPA之“灵活的数字员工”执行器
Parsing of string class intern() method
狼叔来找翻译人员了--plato--持续翻译中.....
Kanban Quick Start Guide
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]
X86 assembly syntax: at & T and Intel
4 most common automated test challenges and Countermeasures
Open source rule engine - Ice: dedicated to solving flexible and complex hard coding problems
弘玑|数字化时代下,HR如何进行自我变革和组织变革?
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
如果我是pm之 演出电影vr购票展示
selenium预先加载cookie的必要性
分支与循环语句
Uniapp wechat sharing
Note: unordered_ Understanding and use of map
es6数组的使用