当前位置:网站首页>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;
}
边栏推荐
- 【Swoole系列2.1】先把Swoole跑起来
- Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- sql语句优化,order by desc速度优化
- Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
- Getting started with pytest ----- test case rules
- Easy introduction to SQL (1): addition, deletion, modification and simple query
- The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
- 李書福為何要親自掛帥造手機?
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
- Dichotomy (integer dichotomy, real dichotomy)
猜你喜欢
8位MCU跑RTOS有没有意义?
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
declval(指导函数返回值范例)
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
Jerry's access to additional information on the dial [article]
基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
随机推荐
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
Why should Li Shufu personally take charge of building mobile phones?
UDP协议:因性善而简单,难免碰到“城会玩”
d绑定函数
OliveTin能在网页上安全运行shell命令(上)
Markdown grammar - better blogging
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
Pourquoi Li shufu a - t - il construit son téléphone portable?
It doesn't make sense without a distributed gateway
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
IP, subnet mask, gateway, default gateway
【.NET CORE】 请求长度过长报错解决方案
李书福为何要亲自挂帅造手机?
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
李書福為何要親自掛帥造手機?
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
【Android】Kotlin代码编写规范化文档
Redis的五种数据结构
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities