Reverse Array Elements in C

Inplace swap of values stored at the locations with a bit-level trick:

void inplace_swap(int *x, int *y) {
  *y = *x ^ *y;
  *x = *x ^ *y;
  *y = *x ^ *y;
}

Details of this trick can be found at: In-place Swap, in C.

Then, using the following function to reverse the elements in an array:

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]);
  }
}

PAGE UP