54. Swap Two Pointers Using Double Pointers

Back To All Submissions
Previous Submission
Next Submission

Code

#include <stdio.h>

void swap_pointers(int **p1, int **p2) {
    // Your logic here
    int *temp = *p2; //store address pointed by p1
    *p2 = *p1; // p1 now points to what p2 was printing
    *p1 = temp; // p2 now points to what p1 was printing
}

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

    int *p1 = &a;
    int *p2 = &b;

    swap_pointers(&p1, &p2);

    printf("%d %d", *p1, *p2);

    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote