(type *) ((unsigned char *) q - offsetof(type, link))

#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; }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章