Answer to Question #210905 in C for genji 5

Question #210905

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.


1
Expert's answer
2021-06-30T05:05:34-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS