原文: https://beginnersbook.com/2019/02/c-program-to-swap-two-numbers-using-pointers/
在本教程中,我们将编写一个 C 程序,使用指针交换两个数字。我们已经涵盖如何在不使用指针的情况下交换两个数字。
使用指针交换两个数字的 C 示例
/*C program by Chaitanya for beginnersbook.com* Program to swap two numbers using pointers*/#include <stdio.h>// function to swap the two numbersvoid swap(int *x,int *y){int t;t = *x;*x = *y;*y = t;}int main(){int num1,num2;printf("Enter value of num1: ");scanf("%d",&num1);printf("Enter value of num2: ");scanf("%d",&num2);//displaying numbers before swappingprintf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);//calling the user defined function swap()swap(&num1,&num2);//displaying numbers after swappingprintf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);return 0;}
输出:

