25 lines
653 B
C
25 lines
653 B
C
|
#include <stdio.h>
|
||
|
|
||
|
// 这种方式只能交换指针指向的值,不能交换指针本身,而且无法改变实参的值
|
||
|
void swap(int *piA, int *piB)
|
||
|
{
|
||
|
printf("first iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", piA, piB, *piA, *piB);
|
||
|
|
||
|
int *piC;
|
||
|
piC = piA;
|
||
|
piA = piB;
|
||
|
piB = piC;
|
||
|
printf("after iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", piA, piB, *piA, *piB);
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int iA = 100, iB = 200;
|
||
|
printf("main first iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", &iA, &iB, iA, iB);
|
||
|
|
||
|
int *piA = &iA, *piB = &iB;
|
||
|
swap(piA, piB);
|
||
|
printf("main after iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", &iA, &iB, iA, iB);
|
||
|
|
||
|
return 0;
|
||
|
}
|