为什么这段代码不能编译?
#define container_of(ptr, type, field_name) ( \
(type *) ((char *) (ptr) - offsetof(type, field_name)))
错误
C:\list.h:37: error: expected expression before ')' token (type * )((char *) (ptr) - offsetof(type, field_name))) ^
最佳答案
编译器在宏定义上产生错误的最可能原因是定义提前结束并且第二行被作为代码进行编译。如果 \
和行尾之间存在额外的空白字符,就会发生这种情况。如果文件从 Windows 环境移动到 Linux 或 Mac 系统,则可能是 或制表符,也可能是意外的回车符。
使宏适合一行:
#define container_of(ptr, t, m) ((t*)((char*)(ptr) - offsetof(t, m)))
关于c - 宏未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34364637/