#include <stdio.h>
#include <stdlib.h>
void swap(int* a, int* b)
{
int c = *a;
*a = *b;
*b = c;
}
void reverse(int* first, int* last)
{
while((first != last) && (first != --last))
{
swap(first++, last);
}
}
int* reverse_copy(int* first, int size)
{
int* result = (int*)malloc(size * sizeof(int));
for(int i = 0; i < size; ++i)
{
result[i] = first[size - i - 1];
}
return result;
}
int main()
{
int a[] = {0,1,2,3,4,5,6,7,8,9};
reverse(a, &a[10]);
for(int i = 0; i < 10; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
int* b = reverse_copy(a, 10);
for(int i = 0; i < 10; ++i)
{
printf("%d ", b[i]);
}
free(b);
}
OUT:
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9