Skip to main content
Code Review

Return to Revisions

2 of 9
added 199 characters in body
Valeriy
  • 161
  • 11

Reverse array in C

I wrote a function to reverse the array. The first argument is an iterable object, the second argument is the size of the array element in bytes, the third argument is the length of the array.

void reverse(char *object, int size, int length) {
 int i, j; 
 for(i=0, j=length-1; i < j; i++, j--) {
 *(object+(i * size)) = *(object+(i * size)) ^ *(object+(j * size));
 *(object+(j * size)) = *(object+(i * size)) ^ *(object+(j * size));
 *(object+(i * size)) = *(object+(j * size)) ^ *(object+(i * size));
 }
}

Using this function leads to warnings such as:

malloc.c:20:5: warning: passing argument 1 of ‘reverse’ from incompatible pointer type [enabled by default]
 reverse(a, sizeof(int), 8);

Could this implementation cause errors? How can I improve it?

Valeriy
  • 161
  • 11
lang-c

AltStyle によって変換されたページ (->オリジナル) /