题目:有两个指针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所示:
图1 运行效果