#include <stdio.h> #include<stdlib.h> void swap(int *pa, int *pb) { int tmp = *pa; *pa = *pb; *pb= tmp; } int main() { int n1 = 0; int n2 = 0; char *p1 = &n1; char *p2 = &n2; printf("请输入两个数:"); scanf_s("%d%d", &n1, &n2); swap(p1,p2); printf("n1=%d n2=%d\n", n1, n2); system("pause"); return 0; }
运行结果:
请输入两个数:45 12
12 45
请按任意键继续. . .
使用函数实现两个数的交换