VC++实现Linux内核链表中的container_of的宏

#include<stdio.h>


#define container_of(addr,type,field) ((type*)((unsigned char*)addr - (unsigned long)&((type*)0)->field))
   struct test_struct {
         
         
         float f1;
         int num;
         char ch;
  }; 
  int main(void)
  {
          struct test_struct *test_struct;
          struct test_struct init_struct ;//={12,'a',12.3};
           int *ptr_num = &init_struct.num;
          init_struct.f1=(float)12.3;
          init_struct.ch='a';
          init_struct.num=12;
         
          test_struct = container_of(ptr_num,struct test_struct,num);
          printf("test_struct->num =%d\n",test_struct->num);
          printf("test_struct->ch =%c\n",test_struct->ch);
          printf("test_struct->f1 =%f\n",test_struct->f1);
          while(1);
          return 0;
  }

 

posted @ 2013-11-11 10:50  M.Bing  阅读(497)  评论(0)    收藏  举报