当前位置:网站首页>指针传参传值等的几种使用实例
指针传参传值等的几种使用实例
2022-04-22 05:44:00 【What’smean】
代码:
#include<iostream>
using namespace std;
//第一个函数以值传递的方式使用指针,所有改变都局限于函数内部,
//当函数执行完毕后既不会改变指针本身的值,也不会改变指针所指的内容。
void SwapPoint1(int *p,int *q){
int * tmp = p;
p = q;
q = tmp;
}
//第二个函数同样以值传递的方式使用指针,
//但是在函数内部通过解引用的方式直接访问内存并修改了指针所指的内容。
void SwapPoint2(int *p,int *q){
int tmp = *p;
*p = *q;
*q = tmp;
}
//函数的参数形式是int *&,
//其含义是,该参数是一个引用,引用的对象是内存中的一个int 指针,
//使用这种方式可以把指针当成对象,交换指针本身的值。需要注意的是,
//最后一个函数既然交换了指针,当然解引用该指针所得的结果也会相应发生改变。
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<<"交换前p的值是:"<< p<<"\t"<<"q的值是:"<<q<<endl;
cout<<"交换前p所指的值是:"<<*p<<"\t"<<"q所指的值是:"<<*q<<endl;
cout<<endl;
SwapPoint1(p,q);
cout<<"交换后p的值是:"<< p<<"\t"<<"q的值是:"<<q<<endl;
cout<<"交换后p所指的值是:"<<*p<<"\t"<<"q所指的值是:"<<*q<<endl;
cout<<endl;
a = 5,b = 10;
p = &a,q = &b;
cout<<"交换前p的值是:"<< p<<"\t"<<"q的值是:"<<q<<endl;
cout<<"交换前p所指的值是:"<<*p<<"\t"<<"q所指的值是:"<<*q<<endl;
cout<<endl;
SwapPoint2(p,q);
cout<<"交换后p的值是:"<< p<<"\t"<<"q的值是:"<<q<<endl;
cout<<"交换后p所指的值是:"<<*p<<"\t"<<"q所指的值是:"<<*q<<endl;
cout<<endl;
a = 5,b = 10;
p = &a,q = &b;
cout<<"交换前p的值是:"<< p<<"\t"<<"q的值是:"<<q<<endl;
cout<<"交换前p所指的值是:"<<*p<<"\t"<<"q所指的值是:"<<*q<<endl;
cout<<endl;
SwapPoint3(p,q);
cout<<"交换后p的值是:"<< p<<"\t"<<"q的值是:"<<q<<endl;
cout<<"交换后p所指的值是:"<<*p<<"\t"<<"q所指的值是:"<<*q<<endl;
return 0;
}
输出:
交换前p的值是:0x66ff24 q的值是:0x66ff20
交换前p所指的值是:5 q所指的值是:10
交换后p的值是:0x66ff24 q的值是:0x66ff20
交换后p所指的值是:5 q所指的值是:10
交换前p的值是:0x66ff24 q的值是:0x66ff20
交换前p所指的值是:5 q所指的值是:10
交换后p的值是:0x66ff24 q的值是:0x66ff20
交换后p所指的值是:10 q所指的值是:5
交换前p的值是:0x66ff24 q的值是:0x66ff20
交换前p所指的值是:5 q所指的值是:10
交换后p的值是:0x66ff20 q的值是:0x66ff24
交换后p所指的值是:10 q所指的值是:5
请按任意键继续. . .
版权声明
本文为[What’smean]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42492218/article/details/124298342
边栏推荐
- Photoresist for learning of Blue Bridge Cup embedded expansion board
- js等待异步执行完成后在执行
- STM32 learning note 5 - Calculation of relative position of RGB screen
- Developing Postgres custom function with C language
- wgs84坐标转换,地图拾取wgs84坐标工具推荐
- Geojson file and ShapeFile file single conversion gadget
- Interaction method I between STM32 single chip microcomputer and ld3320 voice module
- qt.qpa.plugin: Could not find the Qt platform plugin “xcb“ in ““
- 标准输入、标准输出、标准错误 重定向及管道
- Chorme debugging tool
猜你喜欢

MODBUS协议简记

Error in QT: undefined reference to ` widget:: settime()‘

IWDG

Initial knowledge of data linked list

AD5724 bipolar ADC

蓝桥杯嵌入式扩展板学习之DS18B20

QT学习之代码颜色区别

LeetCode: 剑指 Offer 29. 顺时针打印矩阵.

蓝桥杯嵌入式扩展板学习之数码管

On the relationship between the Euler angle of RT matrix and the type of pose in Halcon in hand eye calibration
随机推荐
Clock
Oracle uses C language to write custom functions
Geojson file and ShapeFile file single conversion gadget
LeetCode: 322. 零钱兑换(动态规划,递归,备忘录递归以及回溯)
QWbEngneView和QWebChanel的使用。
线程内容学习
Installation of QT learning
2020-10-28
Part 74 leetcode exercise (VII) 7 Integer inversion
opencv近期学习测试代码
蓝桥杯嵌入式扩展板学习之光敏电阻
jeecgboot-online在线开发3
FILE写入数据到本地
STM32 learning note 1 - the simplest GPIO
cmd
Photoresist for learning of Blue Bridge Cup embedded expansion board
opencv图像增强
jeecgboot-online在线开发2
ADC
常见面试问题 - 2(计算机网络)