已知结构体成员地址,算出该结构体首地址
@2018-09-28
code
1 #include <stdio.h> 2 3 4 #define rt_container_of(ptr, type, member) \ 5 ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member))) 6 7 8 struct _structure 9 { 10 char a; 11 short b; 12 int c; 13 float d; 14 double e; 15 }; 16 17 18 int main(void) 19 { 20 struct _structure obj; 21 22 printf("%#X:\t%#X\t%#X\t%#X\t%#X\t%#X\t\n", &obj, &obj.a, &obj.b, &obj.c, &obj.d, &obj.e); 23 24 25 printf("%#X\t%#X\t%#X\n", (char *)(&obj.c), &((struct _structure *)0)->c, rt_container_of(&obj.c, struct _structure, c)); 26 }
再牛逼的梦想也架不住傻逼似的坚持