C memcpy in reverse
This works for copying int
s in reverse:
void reverse_intcpy(int *restrict dst, const int *restrict src, size_t n){ size_t i; for (i=0; i < n; ++i) dst[n-1-i] = src[i];}
Just like memcpy()
, the regions pointed-to by dst
and src
must not overlap.
If you want to reverse in-place:
void reverse_ints(int *data, size_t n){ size_t i; for (i=0; i < n/2; ++i) { int tmp = data[i]; data[i] = data[n - 1 - i]; data[n - 1 - i] = tmp; }}
Both the functions above are portable. You might be able to make them faster by using hardware-specific code.
(I haven't tested the code for correctness.)