当前位置:网站首页>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;
}
边栏推荐
- High precision operation
- C语言通过指针交换两个数
- std::true_ Type and std:: false_ type
- Interesting - questions about undefined
- VR全景婚礼,帮助新人记录浪漫且美好的场景
- MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
- Jerry's updated equipment resource document [chapter]
- Mysqlimport imports data files into the database
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
- Getting started with pytest ----- allow generate report
猜你喜欢
【.NET CORE】 请求长度过长报错解决方案
SAP UI5 框架的 manifest.json
F200——搭载基于模型设计的国产开源飞控系统无人机
declval(指导函数返回值范例)
EasyCVR授权到期页面无法登录,该如何解决?
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
虚拟机VirtualBox和Vagrant安装
1700C - Helping the Nature
推荐好用的后台管理脚手架,人人开源
随机推荐
Growth of operation and maintenance Xiaobai - week 7
分布式不来点网关都说不过去
High precision operation
It doesn't make sense without a distributed gateway
李書福為何要親自掛帥造手機?
Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
Unity particle special effects series - treasure chest of shining stars
Alibaba brand data bank: introduction to the most complete data bank
面试突击63:MySQL 中如何去重?
二分(整数二分、实数二分)
關於這次通信故障,我想多說幾句…
Shell input a string of numbers to determine whether it is a mobile phone number
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
趣-关于undefined的问题
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
Dichotomy (integer dichotomy, real dichotomy)
STM32按键状态机2——状态简化与增加长按功能
J'aimerais dire quelques mots de plus sur ce problème de communication...
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
面试突击62:group by 有哪些注意事项?