Q:

C Pointers - swap

I'm playing with pointers in C...
Instead of the regular swap that receives pointers to values and swaps values, I'm trying to write a swap that receives pointers to values, retrives pointers of the pointers and swap pointers to values.
Here's the code...

Swap function:
void swap(void *left, void *right)
{
puts("\nswap function:\n\n");

printf("&left :  %i   -   left:  %i   -   *left(should be a):  %i\n",
            &left, left, *((int *)left));
printf("&right : %i   -   right: %i   -   *right(should be b): %i\n\n",
            &right, right, *((int *)right));

void *PNT_left =  (void *) &left;
void *PNT_right = (void *) &right;

puts("Before swap:");
printf("PNT_left:  %i   -   left: %i\n", (int *)PNT_left, *((int *)PNT_left));
printf("PNT_right: %i   -   right: %i\n\n", (int *)PNT_right, *((int *)PNT_right));

/* swap pointers of pointers*/
void *tmpPNT = PNT_left;
PNT_left = PNT_right;
PNT_right = tmpPNT;

puts("Pointer of pointer swapped:");
printf("PNT_left:  %i   -   left: %i\n", (int *)PNT_left, *((int *)PNT_left));
printf("PNT_right: %i   -   right: %i\n\n", (int *)PNT_right, *((int *)PNT_right));

/* swap pointers*/
tmpPNT = (void *)right;
*((int *)PNT_left) = (int *)left;
*((int *)PNT_right) = (int *)tmpPNT;

puts("Pointer swapped:");
printf("PNT_left:  %i   -   left: %i\n", (int *)PNT_left, *((int *)PNT_left));
printf("PNT_right: %i   -   right: %i\n\n", (int *)PNT_right, *((int *)PNT_right));

puts("Info:");
printf("&left :  %i   -   left:  %i   -   *left:  %i\n",
    &left, left, *((int *)left));
printf("&right : %i   -   right: %i   -   *right: %i\n\n", 
    &right, right, *((int *)right));

puts("End Swap function.\n\n\n");
return;
}

Main:
int main()
{
    int a = 1, b = 2;

    puts("Before - Info:");
    printf(" a: %i   -    b: %i\n", a, b);
    printf("*a: %i   -   *b: %i\n\n", &a, &b);

    swap(&a, &b);

    puts("After - Info:");
    printf("*a: %i   -   *b: %i\n", &a, &b);
    printf(" a: %i   -    b: %i\n", a, b);
    return 0;
}

Output:
Before - Info:
 a: 1   -    b: 2
*a: -346930776   -   *b: -346930772

Swap function:

&left :  -346930760   -   left:  -346930776   -   *left(should be a):  1
&right : -346930752   -   right: -346930772   -   *right(should be b): 2

Before swap:
PNT_left:  -346930760   -   left: -346930776
PNT_right: -346930752   -   right: -346930772

Pointer of pointer swapped:
PNT_left:  -346930752   -   left: -346930772
PNT_right: -346930760   -   right: -346930776

Pointer swapped:
PNT_left:  -346930752   -   left: -346930776
PNT_right: -346930760   -   right: -346930772

Info:
&left :  -346930760   -   left:  -346930772   -   *left:  2
&right : -346930752   -   right: -346930776   -   *right: 1

End Swap function.

*a: -346930776   -   *b: -346930772
 a: 1   -    b: 2

Why a and b haven't been swapped? I've tried it also in main, so it's not related to passing by ref or value.

And why if I try to print out *((int *) *((int *)PNT_left)) the program reached SEGMENTATION FAULT while print out *((int) left) works, since at the beginning *((int *)PNT_left)) = left?

Thanks guys!

A:

In 
void *PNT_left =  (void *) &left;
void *PNT_right = (void *) &right;

you take addresses of local variables as pointers are passed by value in C. You have to pass addresses of pointers if you want change the pointers in function and keep this change visible outside of the function.
Here you can find your code corrected.
You should add this in line 33
/* swap values*/
int tmp = *((int*)(*left));
*((int *)(*left)) = *((int *)(*right));
*((int *)(*right)) = tmp;

should you wish to exchange data too.

