This is the macro definition:
/**
* 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)))
I don't understand why ptr is casted to (char *)
. Can't I just subtract the offset of member
from ptr
? Like this:
#define list_entry(ptr, type, member) \
((type *)((ptr)-(unsigned long)(&((type *)0)->member)))
Thanks!
No. Pointer arithmetic is equivalent to:
ptr[addend]
not
(ptr_type *)((unsigned long)&ptr + addend)
The latter requires an explicit cast to char *
(as that is the unit of memory) to manipulate a pointer's value directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.