Pointer- Swap Value

Code

#include <stdio.h>

void swap(int *p1, int *p2) {
    // Your logic here
    int temp=*p1; //get the value at address p1 and store in temp
    *p1=*p2; //put the value at p1 in the address p2
    *p2=temp; //put the value at p2 in p1 address
}

int main() {
    int a, b;
    scanf("%d %d", &a, &b);

    swap(&a, &b);

    printf("a = %d ", a);
    printf("b = %d", b);

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10 20

Expected Output

a = 20 b = 10