containof 是Linux 驱动常用的宏,下面是一个例子。在vs2019上不能编译 ,gcc可以编译执行?
#include <stddef.h>
#include <stdio.h>
/*#define offsetof(TYPE, MEM) ((size_t) &((TYPE *)0)->MEM)
*/
#define container_of(ptr, type, mem) ({ \
const typeof(((type*)0)->mem) * __mptr = (ptr); \
(type*)((char*)__mptr - offsetof(type, mem)); })
int main()
{
struct Persion {
int age;
int score;
};
struct Persion wangyiPiv = { 11,100 };
struct HschoolPersion {
int privinceCode;
int num;
struct Persion* persion;
char nation;
};
struct HschoolPersion wangyi = { 0,0,&wangyiPiv };
struct HschoolPersion* wangyiAlis = container_of(&(wangyi.persion), struct HschoolPersion, persion);
printf("wangyi = %x , wangyiAlis=%x", &wangyi, wangyiAlis);
}
|