有两个指针pa,pb分别指向有两个数,a,b,请写一个函数交换两个指针的指向,也就是让pa指向b,让pb指向a

时间:2023-02-06 00:35:54

题目:有两个指针pa,pb分别指向有两个数,a,b,请写一个函数交换两个指针的指向,也就是让pa指向b,让pb指向a,具体实现如下:

#include<stdlib.h>
#include<stdio.h> int swap_ptr(int ** pA, int ** pB)
{
    if (pA == NULL || pB == NULL)
    {
        return 0;
    }
    int * pTemp = *pA;
    *pA = *pB;
    *pB = pTemp;
    return 1;
} int main()
{
    int a = 3;
    int b = 2;
    int * pA = &a;
    int * pB = &b;
    printf("pA:%p,pB:%p,*pA=%d,*pB=%d\n", pA, pB, *pA, *pB);
    if (swap_ptr(&pA, &pB) == 0)
    {
        printf("交换失败.\n");
    }
    else
    {
        printf("pA:%p,pB:%p,*pA=%d,*pB=%d\n", pA, pB, *pA, *pB);
    }
    system("pause");
    return 0;
}

运行效果如图1所示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqUAAABbCAIAAADFvWslAAAQRUlEQVR4nO2d7W8cVxWH/efsH+Dv/mhhBCxqDUSossGq1CCEhCwqgUArQKgvoS1tXeEUSmlK2yiUFmcbNWmaZJ0Kavoi1aYuDRFQF6mtiO04LzQJ/WA+7Nude+85987s7M54/Dz6qVrP3jn33LOT+5s7M7sdO3m4jhBCCKFqa+zk4frOykGEEEIIZdbr9XoZJKWH3yOEEEI5qHCnx+8RQgihoatwp8fvEUIIoaGrcKfH7xFCCKGhq3Cnx+8RQgihoatwp8fvEUIIoRSqqUh7Fe70+D1CCCGUQiP2+93d3d3dXX3L6Px+5Y93n3r2+zF6/cXvbf/5rsI/LYQQQiibFFMf0vreNPhBzD4Hv3/+xeNvvbP+1jvvOlp/e3X97dX146fOHz+5fPzk8rPHXvjXqW8U/mmhsmhxojY5tSY2mJ6X3l2aqtcmmosTtdpEs7NxZmEyeZo9N92c85x91xsz/d77u3d7NBsIWmuM12rjC0tFV6/k5a2KmnM1tYxo32n0ft+z+QHNPge/P9tav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