What happens when I modify the swap function this way?

Viewed 386

what happens when we modify the swap function this way ? I know it doesn't work but what exactly is going on ? I'm not understanding what I actually did ?

#include <stdio.h>

void swap(int*, int*);

int main(){
   int x=5,y=10;
   swap(&x, &y);
   printf("x:%d,y:%d\n",x,y);
   return 0;
 }

void swap(int *x, int *y){ 
   int* temp;
   temp=x;
   x=y;
   y=temp;
}
3 Answers

In the function

void swap(int* x, int* y){ 
    int* temp;
    temp=x;
    x=y;
    y=temp;
}

you just swap the pointer values for the two function arguments.

If you want to swap their values you need to implement it like

void swap(int* x, int* y){ 
    int temp = *x;  // retrive the value that x poitns to
    *x = *y;        // write the value y points to, to the memory location x points to
    *y = temp;      // write the value of tmp, to the memory location x points to
}

that way the swap function swaps the value for the referenced memory locations.

void swap(int *x, int *y){ 
   int* temp;
   temp = x;
   x = y;
   y = temp;
}

swap() takes two pointers to int here but internally only swaps the values of the local pointers x and y - the addresses of the objects they do point to. - Means that at the end of swap() before it returns, x points to the object y have pointed to and y points to the object x have pointed to.

There is no effect in the caller at the objects x and y point to.


If you want to swap the values of the objects which xand y point to, you need to define swap() like that:

void swap(int *x, int *y){ 
   int temp;
   temp = *x;     // temp gets the int value of the int object x is pointing to.
   *x = *y;       // x gets the int value of the int object y is pointing to.
   *y = temp;     // y gets the int value of temp (formerly x).
}

Example program (Online Example):

#include <stdio.h> 

void swap(int *x, int *y){ 
   int temp;
   temp = *x;     // temp gets the int value of the int object x is pointing to.
   *x = *y;       // x gets the int value y of the int object y is pointing to.
   *y = temp;     // y gets the int value of temp (formerly x).
}

int main (void)
{
    int a = 1, b = 2;
    printf("Before the swap() function:\n");
    printf("a = %d b = %d\n\n",a,b);

    swap(&a,&b);

    printf("After the swap() function:\n");
    printf("a = %d b = %d",a,b);

    return 0;
}

Output:

Before the swap() function:
a = 1 b = 2

After the swap() function:
a = 2 b = 1

Within the function its two parameters that hold copies of values of the passed arguments are swapped.

As the function deals with copies of arguments the original arguments (passed pointers to x and y) that are temporary objects will not be swapped.

To swap values of pointers to x and y you have to define in main such pointers initializing them by the addresses of x and y and pass them to the function by reference through pointers to the pointers.

Here is a demonstrative program.

#include <stdio.h>

void swap( int **ppx, int **ppy )
{
    int *tmp = *ppx;
    *ppx = *ppy;
    *ppy = tmp;
}

int main(void) 
{
    int x=5, y=10;

    int *px = &x;
    int *py = &y;

    printf( "*px = %d, *py = %d\n", *px, *py );

    swap( &px, &py );

    printf( "*px = %d, *py = %d\n", *px, *py );

    return 0;
}

Its output is

*px = 5, *py = 10
*px = 10, *py = 5

See also my answer to this question Pointer Confusion: swap method in c

Related