C语言:交换两个指针变量存储位置处的值
#include <stdio.h>
void inplace_swap(int *x, int *y) //交换指针变量x和y所指向的存储位置处存放的值
{ //原理为对于任一位向量a,有a ^ a = 0
*y = *x ^ *y;
*x = *x ^ *y;
*y = *x ^ *y;
}
void reverse_array(int a[], int length)
{
int first, last;
for (first = 0, last = length-1; first<last; first++, last--)
inplace_swap(&a[first], &a[last]);
for (first=0; first<length; first++)
printf("%d", a[first]);
printf("\n");
}
int main()
{
int a[4] = {1,2,3,4};
int b[5] = {1,2,3,4,5};
reverse_array(a, 4);
reverse_array(b, 5);
return 0;
}