当前位置:网站首页>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;
}
边栏推荐
- Interpretation of Flink source code (I): Interpretation of streamgraph source code
- 虚拟机启动提示Probing EDD (edd=off to disable)错误
- There is a gap in traditional home decoration. VR panoramic home decoration allows you to experience the completion effect of your new house
- Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
- Xin'an Second Edition; Chapter 11 learning notes on the principle and application of network physical isolation technology
- [introduction to MySQL] third, common data types in MySQL
- 2022年大厂Android面试题汇总(一)(含答案)
- Concept and basic knowledge of network layering
- Automatic operation and maintenance sharp weapon ansible Foundation
- Flink parsing (VII): time window
猜你喜欢
node の SQLite
基于STM32+华为云IOT设计的智能路灯
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
[translation] principle analysis of X Window Manager (I)
Automatic operation and maintenance sharp weapon ansible Foundation
全网最全tcpdump和Wireshark抓包实践
Solr appears write Lock, solrexception: could not get leader props in the log
Pytest learning ----- pytest confitest of interface automation test Py file details
Basic configuration and use of spark
Zen integration nails, bugs, needs, etc. are reminded by nails
随机推荐
[introduction to MySQL] third, common data types in MySQL
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
Xin'an Second Edition: Chapter 25 mobile application security requirements analysis and security protection engineering learning notes
Nodejs 开发者路线图 2022 零基础学习指南
TCP连接不止用TCP协议沟通
Remote code execution penetration test - B module test
Distinguish between basic disk and dynamic disk RAID disk redundant array
How to submit data through post
How to use scroll bars to dynamically adjust parameters in opencv
Pytest learning ----- pytest confitest of interface automation test Py file details
分布式不来点网关都说不过去
Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
基于STM32+华为云IOT设计的智能路灯
Interview assault 63: how to remove duplication in MySQL?
[translation] principle analysis of X Window Manager (I)
Wordcloud colormap color set and custom colors
Zen integration nails, bugs, needs, etc. are reminded by nails
connection reset by peer
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
C # nanoframework lighting and key esp32