What about this code, is it standard ? I've seen that kind of code in a Visual C++ header, it can calculate the offsets of some structure members but how does it works exactly ?
#include <iostream>
struct Foo
{
int a;
int b;
int c;
};
int main(int argc, char** argv)
{
int* i1 = (&((Foo*)0)->a);
int* i2 = (&((Foo*)0)->b);
int* i3 = (&((Foo*)0)->c);
std::cout << "i1 = " << i1 << "\ni2 = " << i2 << "\ni3 = " << i3 << "\n";
return 0;
}
Results : i1 = 0, i2 = 4, i3 = 8
Edit :
Just remembered where I saw this code before. It's in WinNT.h
#define FIELD_OFFSET(type, field) ((LONG)(LONG_PTR)&(((type *)0)->field))