#include "stdio.h" void swap(int*p1,int*p2) { int tmp=*p1; *p1=*p2; *p2=tmp; } void main() { int p1=1; int p2=2; int *num1=&p1; int *num2=&p2; swap(num1,num2); printf("p1=%d p2=%d\n",p1,p2); }
输出结果:p1=2,p2=1
2.不允许创建临时变量,交换两个数的内容
#include "stdio.h" int main() { int num1=1; int num2=2; printf("num1=%d\n",num1); printf("num2=%d\n",num2); num1=num1+num2; num2=num1-num2; num1=num1-num2; printf("num1=%d\n",num1); printf("num2=%d\n",num2); return 0; }
输出结果:p1=2,p2=1
时间: 2024-10-13 17:30:58