将一个数组中的元素头尾两端对调
#include <stdio.h> void inplace_swap(int *x, int *y) { printf("--------------\n"); printf("x = %d, y = %d\n", *x, *y); *y = *x ^ *y; printf("x = %d, y = %d\n", *x, *y); *x = *x ^ *y; printf("x = %d, y = %d\n", *x, *y); *y = *x ^ *y; printf("x = %d, y = %d\n", *x, *y); } void reverse_array(int a[], int cnt) { int first, last; for(first=0, last=cnt-1; first<last; first++, last--) { inplace_swap(&a[first], &a[last]); } } int main() { int a[]={1,2,3,4}; int i; printf("交换前: \n"); for(i=0;i<sizeof(a)/sizeof(a[0]);i++) { printf("a[%d] = %d, ", i, a[i]); } printf("\n"); reverse_array(a, sizeof(a)/sizeof(a[0])); printf("\n交换后: \n"); for(i=0;i<sizeof(a)/sizeof(a[0]);i++) { printf("a[%d] = %d, ", i, a[i]); } printf("\n"); return 0; }