r/linuxdev • u/sbay • Oct 27 '17
Container_of and offset_of understanding
Could someone explain to me these two macros?
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})I understand everything except why offset_of is of (size_t) and container_of has (char *)?
How would (char *) -(size_t) work in this macro?
I would have expected both of them to be of the same type. like char * for example.
4
Upvotes
View all comments
Show parent comment
2
u/sbay Oct 27 '17
Thanks for responding. I guess I couldn't frame my question right. I understand that the return type is type*
My main confusion is why we selected (char *) - (size_t)?
Why not cast the address in offsetof to char * and not to size_t.
So basically what I am asking is: why this is not ok?
So that in container_of we would have (char *) - (char *), which makes more sense to me.