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:
not
The latter requires an explicit cast to
char *(as that is the unit of memory) to manipulate a pointer’s value directly.