当前位置:网站首页>C语言通过指针交换两个数
C语言通过指针交换两个数
2022-07-06 09:43:00 【芣苢的成长之路】
#include<stdio.h>
void swap1(int *p1,int *p2){
//p1,p2分别对应a,b的地址,*p1和*p2分别对应a和b的值
int p;//p是一个变量
p = *p1;
*p1 = *p2;
*p2 = p;
//通过地址进行值的交换操作,实际交换的是a,b的值
}
void swap2(int *p1,int *p2){
//p1,p2分别对应c,d的地址,*p1和*p2分别对应c和d的值
//这一步的操作是把p1 p2这两个地址当做形参进行操作
int *p;//p是一个指针
p = p1;
p1 = p2;
p2 = p;
//这个是形参交换,和原本的c d没有关系
}
int main() {
int a = 5,b = 10;
printf("原a b:%d %d\n",a,b);
swap1(&a,&b);
printf("swap1后a b:%d %d\n",a,b);
int c = 6,d = 8;
printf("原c d:%d %d\n",c,d);
swap2(&c,&b);
printf("swap2后c d:%d %d\n",c,d);
return 0;
}

边栏推荐
- Xin'an Second Edition: Chapter 25 mobile application security requirements analysis and security protection engineering learning notes
- The solution that flutterweb browser cannot be rolled back after refreshing
- 分布式不来点网关都说不过去
- Flink analysis (II): analysis of backpressure mechanism
- 一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
- 基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
- RepPoints:可形变卷积的进阶
- Pyspark operator processing spatial data full parsing (4): let's talk about spatial operations first
- 【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
- 10 advanced concepts that must be understood in learning SQL
猜你喜欢

Grafana 9 正式发布,更易用,更酷炫了!

Uipath browser performs actions in the new tab

Pytest learning ----- detailed explanation of the request for interface automation test

Interview shock 62: what are the precautions for group by?

Pytest learning ----- pytest confitest of interface automation test Py file details

C WinForm series button easy to use

一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升

Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter

EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
![[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias](/img/03/ece7f7b28cd9caea4240635548c77f.jpg)
[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias
随机推荐
Zen integration nails, bugs, needs, etc. are reminded by nails
Manifest of SAP ui5 framework json
Essai de pénétration du Code à distance - essai du module b
TCP连接不止用TCP协议沟通
Hongmeng introduction and development environment construction
Flink parsing (IV): recovery mechanism
Solrcloud related commands
2022年大厂Android面试题汇总(二)(含答案)
Pytorch extract middle layer features?
传统家装有落差,VR全景家装让你体验新房落成效果
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
Sqoop I have everything you want
The shell generates JSON arrays and inserts them into the database
Growth of operation and maintenance Xiaobai - week 7
2022年大厂Android面试题汇总(一)(含答案)
PySpark算子处理空间数据全解析(5): 如何在PySpark里面使用空间运算接口
李书福为何要亲自挂帅造手机?
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability