Swap the values of two variables with pointers (10 points)
Topic content:
Swap the values of two variables with pointers
Main function reference: | int main( ) { int a,b; scanf("%d%d",&a,&b); swap(&a,&b); printf("%d %d",a,b); return 0; } |
Input format:
two variables
Output format:
two variables
Input sample:
5 8
Sample output:
8 5
code
#include "stdio.h" // swap the values of the two variables void swap2(int *p1,int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } intmain() { // int c = 3,d = 4; // printf("swap2 before swapping cd: %d,%d\n",c,d); int c,d; scanf("%d %d",&c,&d); swap2(&c,&d); // printf("swap2 after swapping cd: %d,%d\n",c,d); printf("%d %d",c,d); }