Skip to main content
Code Review

Return to Question

added 3 characters in body
Source Link
Alex L
  • 5.8k
  • 2
  • 26
  • 69

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

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

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

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(void *object, int size, int length) {
 int i;
 int j; 
 int k;
 
 for(i=0, j=length-1; i < j; i++, j--) {
 for(k=0; k<size; k++){
 *((char*)object+(i * size)+k) = *((char*)object+(i * size)+k) ^ *((char*)object+(j * size)+k);
 *((char*)object+(j * size)+k) = *((char*)object+(i * size)+k) ^ *((char*)object+(j * size)+k);
 *((char*)object+(i * size)+k) = *((char*)object+(j * size)+k) ^ *((char*)object+(i * size)+k);
 }
 }
}

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

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

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

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

edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Removed old (non-working) code. Code was updated before the first answer was written.
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

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?

Update including a fix for numbers more than 255, as pointed out in a (now-deleted) comment:

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

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

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?

Update including a fix for numbers more than 255, as pointed out in a (now-deleted) comment:

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

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(void *object, int size, int length) {
 int i;
 int j; 
 int k;
 
 for(i=0, j=length-1; i < j; i++, j--) {
 for(k=0; k<size; k++){
 *((char*)object+(i * size)+k) = *((char*)object+(i * size)+k) ^ *((char*)object+(j * size)+k);
 *((char*)object+(j * size)+k) = *((char*)object+(i * size)+k) ^ *((char*)object+(j * size)+k);
 *((char*)object+(i * size)+k) = *((char*)object+(j * size)+k) ^ *((char*)object+(i * size)+k);
 }
 }
}

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

added 85 characters in body
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311
Loading
Rollback to Revision 3
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311
Loading
Rollback to Revision 2
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311
Loading
added 588 characters in body
Source Link
Valeriy
  • 161
  • 11
Loading
added 199 characters in body
Source Link
Valeriy
  • 161
  • 11
Loading
Source Link
Valeriy
  • 161
  • 11
Loading
lang-c

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