struct中如何用成员的地址找到结构首地址

来源:互联网 发布:oracle minus 优化 编辑:程序博客网 时间:2024/04/30 07:23
 #include <stdio.h>  
   
 #define abc(s,m) (size_t)&(((s*)0)->m)  
 #define bcd(ptr, type, member) ({const typeof(((type*)0)->member)* __mptr=(ptr);\  
 (type*)((char*)__mptr-abc(type,member));})  
   
 struct test{  
     int a;  
     char b;  
     long t;  
 };  
 int main()  
 {  
    struct test *p;  
     struct test m;  
     m.a = -1;  
     m.t = 37;  
     p = bcd(&(m.t), struct test, t);  
     printf("%d\n", p->a);  
     return 0;  
 }
原创粉丝点击