Search code examples
cpointerspass-by-referenceswapfunction-definition

Pointer Confusion: swap method in c


#include<stdio.h>
void swap(int *a,int *b){
    int p=*b;
    *b=*a;
    *a=p;

    /*int *p=b;
    b=a;
    a=p;
    */
}

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

Above is the code.

If I put 3 5 as an input, then it should swap its values, and 5 3 should come out as an output. I got my answer by trying int p=*b thing

However I also tried commented part, but it didn't work.

So, I checked their address in swap and in main.

In swap int *a and int *b their address changed

However, when I came back to main, a and b 's addresses were not changed...

So first I thought: is it not changed in main because parameter int *a,int *b is local variable?

But I also learned that when pointers and arguments are used as arguments their value can change unlike other variables...

I really wonder why the second method(commented part) is not swapping the values...


Solution

  • If you want to change in a function original objects you have to pass them to the function by reference.

    In C passing objects by reference means passing them indirectly through pointers that point to the original object.

    Otherwise if you will pass the original objects themselves to the function the function will deal with copies of the objects. It is evident that changing copies does not influence on the original objects.

    It is exactly what happens in this function

    void swap(int *a,int *b){
        int *p=b;
        b=a;
        a=p;
    }
    

    The function deals with copies of pointers passed to the function as argument in this call

    swap(&a,&b);
    

    That is the function indeed swapped values of the two pointers that are declared as its parameters. But they are not the original pointers passed to the function. They are copies of the pointers. So the values of the original pointers were not changed

    The function swap in general can look the following way

    void swap( T *a, T *b )
    {
        T tmp = *a;
        *a = *b;
        *b = tmp;
    }  
    

    where T is same type specifier.

    So if you want to swap objects of the type int then in the above function T will be int and the function will look like

    void swap( int *a, int *b )
    {
        int tmp = *a;
        *a = *b;
        *b = tmp;
    }  
    

    If you want to swap values of pointers of the type int * then T will be int * and the function will look like

    void swap( int **a, int **b )
    {
        int *tmp = *a;
        *a = *b;
        *b = tmp;
    }  
    

    Here is a demonstrative program.

    #include <stdio.h>
    
    void swap1( int *pa, int *pb )
    {
        int tmp = *pa;
        *pa = *pb;
        *pb = tmp;
    }
    
    void swap2( int **ppa, int **ppb )
    {
        int *tmp = *ppa;
        *ppa = *ppb;
        *ppb = tmp;
    }
    
    int main(void) 
    {
        int a = 3, b = 5;
    
        swap1( &a, &b );
    
        printf( "a = %d b = %d\n", a, b );
    
        //  reset again the values of the variables
        a = 3; b = 5;
    
        int *pa = &a, *pb = &b;
    
        swap2( &pa, &pb );
    
        printf( "*pa = %d *pb = %d\n", *pa, *pb );
    
        return 0;
    }
    

    Its output is

    a = 5 b = 3
    *pa = 5 *pb = 3
    

    That is at first in the program two objects of the type int are swapped, So the imagined type specifier T is int.

    Then two pointers that point to the objects a and b are swapped. So the imagined type specifier T int *.

    After swapping the pointers the pointer pa now points to the object b and the pointer pb now points to the object a.