当前位置:网站首页>C language exchanges two numbers through pointers
C language exchanges two numbers through pointers
2022-07-06 18:06:00 【The way of growth of Peng】
#include<stdio.h>
void swap1(int *p1,int *p2){
//p1,p2 They correspond to each other a,b The address of ,*p1 and *p2 They correspond to each other a and b Value
int p;//p It's a variable
p = *p1;
*p1 = *p2;
*p2 = p;
// Exchange values through addresses , The actual exchange is a,b Value
}
void swap2(int *p1,int *p2){
//p1,p2 They correspond to each other c,d The address of ,*p1 and *p2 They correspond to each other c and d Value
// The operation of this step is to put p1 p2 These two addresses operate as formal parameters
int *p;//p It's a pointer
p = p1;
p1 = p2;
p2 = p;
// This is the formal parameter exchange , And the original c d It doesn't matter.
}
int main() {
int a = 5,b = 10;
printf(" primary a b:%d %d\n",a,b);
swap1(&a,&b);
printf("swap1 after a b:%d %d\n",a,b);
int c = 6,d = 8;
printf(" primary c d:%d %d\n",c,d);
swap2(&c,&b);
printf("swap2 after c d:%d %d\n",c,d);
return 0;
}
边栏推荐
- Is it meaningful for 8-bit MCU to run RTOS?
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
- Redis的五种数据结构
- Markdown grammar - better blogging
- Why should Li Shufu personally take charge of building mobile phones?
- Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
- Windows连接Linux上安装的Redis
- Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
- 【.NET CORE】 请求长度过长报错解决方案
- Jerry's watch deletes the existing dial file [chapter]
猜你喜欢
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
队列的实现
Is it meaningful for 8-bit MCU to run RTOS?
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
关于这次通信故障,我想多说几句…
1700C - Helping the Nature
Awk command exercise
F200 - UAV equipped with domestic open source flight control system based on Model Design
Why should Li Shufu personally take charge of building mobile phones?
随机推荐
Jerry's watch reading setting status [chapter]
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
编译原理——自上而下分析与递归下降分析构造(笔记)
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
Manifest of SAP ui5 framework json
The easycvr authorization expiration page cannot be logged in. How to solve it?
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
Awk command exercise
Jerry's updated equipment resource document [chapter]
Will openeuler last long
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
二分(整数二分、实数二分)
30 分钟看懂 PCA 主成分分析
EasyCVR授权到期页面无法登录,该如何解决?
UDP协议:因性善而简单,难免碰到“城会玩”
Interview shock 62: what are the precautions for group by?
I want to say more about this communication failure
Smart street lamp based on stm32+ Huawei cloud IOT design
Summary of Android interview questions of Dachang in 2022 (II) (including answers)