I've just wandered into a situation whereby I'm attempting to traverse a struct in memory that I know nothing about bar its size in bytes and that it ends with a pointer to another struct. In order to get to this other struct, I'm doing:-
The thing is, I had to cast to an unsigned long pointer to get the address, not being able to dereference a void pointer. But I haven't seen anything that says that a pointer is always the same size as a long, plus I think that I may also run into alignment errors.
// pretend that we know that structure is good and size truthful...
void *getstruct(void *structure, size_t size)
return (void *)*(unsigned long *)((unsigned char *)structure + size - sizeof(void *));
Is there a better way to do this?