Pointer Confusion: swap method in c

后端 未结 4 1042
情书的邮戳
情书的邮戳 2021-01-29 11:44
#include
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         


        
4条回答
  •  不思量自难忘°
    2021-01-29 12:25

    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 
    
    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.

提交回复
热议问题