美文网首页
(type *) ((unsigned char *) q -

(type *) ((unsigned char *) q -

作者: 东京的雨不会淋湿首尔 | 来源:发表于2020-05-03 16:01 被阅读0次
    #include <stddef.h>
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    #define q_data(q, type, link) \
        (type *) ((unsigned char *) q - offsetof(type, link))
        
        
    typedef struct b{
        int c;
    }b_t;
    typedef struct a{
        b_t b;
    }a_t;
    
    int main() {
    
        a_t aa;
        b_t* bb= &(aa.b);
        bb->c=1;
    
        printf("%p\n",&aa);
        printf("%p\n",&bb);
        
        a_t* aa_test=nullptr;
        b_t* bb_test;
        bb_test=bb;
    
        aa_test = (a_t *)((unsigned char *)bb_test - offsetof(a_t, b));
       
        printf("%p\n",aa_test);
        
        printf("%d",aa_test->b.c);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:(type *) ((unsigned char *) q -

          本文链接:https://www.haomeiwen.com/subject/gdtmoctx.html