sneakyimp;10961197 wrote:
I'm delighted you've worked on this johana. I think the 3-byte sequences probably have something to do with how the system is using printf to interpret %x on (((char ) &s) + i).
My pleasure. It's been fun 🙂
Perhaps. Still, consider this
double d = 1.234;
printf("%f\n", d); // 1.234
char *c = (char *) &d; // now we have a pointer to the first byte of d
double e = (double) *c; // derefence c, giving the first byte of d, convert this to double
printf("%f\n", e); // 88.0
// this on the other hand creates a double pointer that starts at the first byte of d
// but a double pointer points to an 8 byte memory area, which means that
// dereferencing this gives us the same double as we initially had
double f = *((double *) c);
printf("%f\n", f); // 1.234
return 0;
sneakyimp;10961197 wrote:
To me, "(((char ) &s) + i)" looks a bit like LISP. Sometimes known as "Lots [of] Irritating Single Parentheses".
shiver LISP... never was that fond of it myself. If I knew the operator precedence in C, I could probably get rid of one set of parentheses though...
But, breaking it apart:
Get a char (byte sized) pointer to the address of s, then advance pointer by i bytes, and finally get the value of that byte.
sneakyimp;10961197 wrote:
Since an integer is probably 4 bytes (32 bits) then it's interpreting each char as a full-blown int.
Well, to me that would make the leading characters all 0, not F. And look at this
double d = 1.234;
char *c = (char *) &d;
char ch;
for (int i = 0; i < sizeof(d); ++i) {
ch = *(c + i); // we're really using a byte, not several.
printf("%hx ", ch);
}
printf("\n");
printf("%hx %hx %hx %hx %hx %hx %hx %hx\n", 0x58, 0x39, 0xb4, 0xc8, 0x76, 0xbe, 0xf3, 0x3f);
I certainly must be missing something.
sneakyimp;10961197 wrote:
I see that your function takes a pointer to the item to be reversed. I'm wondering if we could perform the cast as char pointer in the function to reduce the verbosity of usage for this routine.
Seems like we would either reverse extra data (yikes!) or get a seg fault. Is there any way to check sizeof within the function?
In C your only universal option (as in one function for all types) for this, as far as I know (once again, that isn't very far when it comes to C), would be via a macro, since a macro can take any type, thus letting you call REVERSE(var) which calls reverse((char ) &var, sizeof(var)). The other way would be one function per type, which has the upside of keeping type safety. If C++ is an option, you could of course use template functions.
#define REVERSE(x) (reverse((char *) &(x), sizeof(x)))
If you cast to the correct pointer before the function is called, then you no longer know what type you were previously dealing with (inside the function). Sizeof within the function would report either 4 or 8 bytes for the char * pointer depending on 32/64 bits architecture, and 1 byte for the value it points to.
sneakyimp;10961197 wrote:
Also, your sizeof bits when you call this function all appear to refer to sizeof(l) rather than the sizeof for the item to be reversed. Does this look ok?
Copy-paste error. It should of course be as you say, except it really should be char , not void . You could use void as well, but then you'd have to cast to char to dereference it first => more parentheses. The reason for this is that void pointers can point to anything, and to dereference a pointer, the size of the data has to be known.
Edit: I just realized you did cast from void to char inside the function. Still, why not go with char* to begin with?