Reversing with two pointers
left starts at the first element and right at the last. Each pass swaps the two boxes they point at, then both arrows move one step toward each other. They stop when they meet in the middle.
#include <stdio.h>
int main(void) {
int v[5] = {1, 2, 3, 4, 5};
int *left = v;
int *right = v + 4;
while (left < right) {
int tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
printf("%d %d %d %d %d\n", v[0], v[1], v[2], v[3], v[4]);
return 0;
}
Output:
5 4 3 2 1
From the lesson: Pointers