The address of memory location num1 and num2 are passed to function and the pointers *a and *b accept those values. So, the pointer a and b points to address of num1 and num2 respectively. When, the value of pointer is changed, the value in memory location also changed correspondingly. Write a program to swap the values in num1 and num2 using *a and *b.
#include <stdio.h>
int main()
{
int num1, num2, *a, *b, temp;
printf("Enter the value of num1 and num2\n");
scanf("%d%d", &num1, &num2);
printf("Before Swapping\nnum1 = %d\nnum2 = %d\n", num1, num2);
a = &num1;
b = &num2;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nnum1 = %d\nnum2 = %d\n", num1, num2);
return 0;
}
Comments
Leave a comment