Pointer Confusion: swap method in c

后端 未结 4 1100
情书的邮戳
情书的邮戳 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:13

    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.

提交回复
热议问题