我尝试使用 ^= 交换值(我知道最好使用另一个变量来执行此操作),但结果不正确。
#include <stdio.h>
int main() {
int a = 3, b = 5, *pa = &a, **ppa = &pa, *pb = &b, **ppb = &pb;
*pa ^= *pb;
*pb ^= *pa;
*pa ^= *pb;
printf("pointer 1: a = %d, b = %d\n", a, b);
a ^= b ^= a ^= b;
printf("variables: a = %d, b = %d\n", a, b);
*pa ^= *pb ^= *pa ^= *pb;
printf("pointer 2: a = %d, b = %d\n", a, b);
return 0;
}
结果是
pointer 1: a = 5, b = 3
variables: a = 3, b = 5
pointer 2: a = 0, b = 3
我想知道为什么*pa ^= *pb ^= *pa ^= *pb
不能正常工作。谁能告诉我?