Consider following code:
#include <iostream>
struct bar {
double a = 1.0;
int b = 2;
float c = 3.0;
};
void callbackFunction(int* i) {
auto myStruct = reinterpret_cast<bar*>(i) - offsetof(bar, b);
std::cout << myStruct->a << std::endl;
std::cout << myStruct->b << std::endl;
std::cout << myStruct->c << std::endl;
//do stuff
}
int main() {
bar foo;
callbackFunction(&foo.b);
return 0;
}
I have to define a callback function and I want to use some additional information in that function. I defined my own struct and pass the address of a member to the function. In the function I want to "retrieve" the whole struct by casting, but the pointers don't seem to match and I get wrong results. I guess I'm doing something wrong while casting but I'm not sure what?