当前位置:网站首页>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;
}
边栏推荐
- connection reset by peer
- JMeter interface test response data garbled
- Pyspark operator processing spatial data full parsing (4): let's talk about spatial operations first
- Interpretation of Flink source code (I): Interpretation of streamgraph source code
- Establishment of graphical monitoring grafana
- 偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
- 【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
- Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
- 遠程代碼執行滲透測試——B模塊測試
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
猜你喜欢
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
分布式(一致性协议)之领导人选举( DotNext.Net.Cluster 实现Raft 选举 )
Alibaba brand data bank: introduction to the most complete data bank
Pytest learning ----- pytest confitest of interface automation test Py file details
sql语句优化,order by desc速度优化
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
[rapid environment construction] openharmony 10 minute tutorial (cub pie)
Pytorch extract middle layer features?
TCP connection is more than communicating with TCP protocol
Awk command exercise
随机推荐
BearPi-HM_ Nano development environment
Example of batch update statement combining update and inner join in SQL Server
OliveTin能在网页上安全运行shell命令(上)
VR panoramic wedding helps couples record romantic and beautiful scenes
Alibaba brand data bank: introduction to the most complete data bank
PySpark算子处理空间数据全解析(5): 如何在PySpark里面使用空间运算接口
Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
2022年大厂Android面试题汇总(二)(含答案)
Xin'an Second Edition: Chapter 24 industrial control safety demand analysis and safety protection engineering learning notes
Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
It doesn't make sense without a distributed gateway
MySQL 8 sub database and table backup database shell script
Flink parsing (VII): time window
面试突击62:group by 有哪些注意事项?
JMeter interface test response data garbled
The solution that flutterweb browser cannot be rolled back after refreshing
C # nanoframework lighting and key esp32
Sqoop I have everything you want
The shell generates JSON arrays and inserts them into the database
BearPi-HM_ Nano development board "flower protector" case