Macros 宏的容器_的定义

Macros 宏的容器_的定义,macros,linux-kernel,c-preprocessor,Macros,Linux Kernel,C Preprocessor,不是将容器_定义为: #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );}) 为什么这不起作用: #define container_of(ptr, type, member) ({ \

不是将容器_定义为:

#define container_of(ptr, type, member) ({ \
            const typeof( ((type *)0)->member ) *__mptr = (ptr);
            (type *)( (char *)__mptr - offsetof(type,member) );})
为什么这不起作用:

#define container_of(ptr, type, member) ({ \
                    (type *)( (char *)(ptr) - offsetof(type,member) );})

定义中第一行的用法是什么?

它增加了某种程度的类型安全性。在你的第二个版本中,我可以为ptr传递任何东西,它可以很好地编译。在内核版本中,如果为
ptr
传递的指针与
type.member的类型不匹配,至少会收到一条警告