Pointer Confusion: swap method in c

半世苍凉 提交于 2021-02-05 12:29:34

问题


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


回答1:


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.




回答2:


In the second method, you use the local variable that is limited in the scope of the function swap. So, the variable a or b in main function is different with variable a or b that is defined as the argument in the swap function.

When you use the pointer, the swap function will change the value that is pointed by the pointer (it means that the function change the value at the address of a and b that are declared in the main function).




回答3:


Assuming you meant

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

That code just swaps the value of the pointers in the swap() function. That won't swap the addresses around in main() because, as you said, "parameter int *a,int *b is local variable".

When you call the function swap() like this

swap(&a,&b);

the addresses of a and b are passed and become local variables in the swap() function. You can't change the address of a or b - they have a location in memory.

In the code that works

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

you don't change the value of the pointers, you change the values in the memory the pointers point to, which is why that works.

While C is pass-by-value, if you pass the address of something as the value a function can modify something outside its scope because you told the function where that variable is.




回答4:


In C, all variables declared in a function are local to that specific function. So, you wrote something in function swap

int *p=b;

What above code will do is, it will copy the value of b into p. So, when the function swap return, it's local variables p, b and a will vanished.



来源:https://stackoverflow.com/questions/61013632/pointer-confusion-swap-method-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!