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?
- 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?