这是宏定义:
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
Run Code Online (Sandbox Code Playgroud)
我不明白为什么ptr被铸造(char *).我不能只是减去偏移的member距离ptr?像这样:
#define list_entry(ptr, type, member) \
((type *)((ptr)-(unsigned long)(&((type *)0)->member)))
Run Code Online (Sandbox Code Playgroud)
谢谢!
不.指针算术相当于:
ptr[addend]
Run Code Online (Sandbox Code Playgroud)
不
(ptr_type *)((unsigned long)&ptr + addend)
Run Code Online (Sandbox Code Playgroud)
后者需要显式转换char *(因为这是内存单元)直接操作指针的值.