先上代码
#include<cstdio> #include<cstring> #define OFFSET(TYPE, MEMBER, OFF) \ TYPE temp; OFF = (unsigned long)(&(temp.MEMBER)) - (unsigned long)(&(temp)); struct Type_t { char a; int b; double c; }; int main() { int offset = 0; Type_t type; offset = (unsigned long)(&(type.c)) - (unsigned long)((&type)); printf("offset = %d\n", offset); int offset_1 = 0; OFFSET(Type_t, c, offset_1); printf("offset = %d\n", offset_1); return 0; }
offset和offset_1都是求偏移量的。
https://blog.csdn.net/encourage2011/article/details/52463857
https://blog.csdn.net/hairetz/article/details/4084088
原文:https://www.cnblogs.com/wanghao-boke/p/13206413.html