【C指针】交换变量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31650113/article/details/80852598

这个地方我困惑的是&a传入到swap函数里的*p1,&a是读入到了 *p1还是p1,结果看来是p1

#include <stdio.h>
#include <stdlib.h>

void swap(int *p1,int *p2)
{
    printf("%p %d\n",p1,*p1);
    printf("%p %d\n",p2,*p2);
    int temp;
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    printf("%p %d\n",p1,*p1);
    printf("%p %d\n",p2,*p2);
}

int main()
{
    int a = 1,b = 2;
    printf("交换之前:%d,%d\n",a,b);
    printf("交换之前a和b的地址:%p,%p\n",&a,&b);

    swap(&a,&b);
    printf("交换之后:%d,%d\n",a,b);
    printf("交换之后a和b的地址:%p,%p\n",&a,&b);

}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_31650113/article/details/80852598