On Wed, Jun 30, 2004 at 11:25:42 +0300, Momchil Velikov wrote: > >>>>> "Borislav" == Borislav Petkov <petkov@uni-muenster.de> writes: > Borislav> I also have another question: > > Borislav> <from include/linux/kernel.h> > Borislav> 203 /** > Borislav> 204 * container_of - cast a member of a structure out to the containing > Borislav> structure > Borislav> 205 * > Borislav> 206 * @ptr: the pointer to the member. > Borislav> 207 * @type: the type of the container struct this is embedded in. > Borislav> 208 * @member: the name of the member within the struct. > Borislav> 209 * > Borislav> 210 */ > Borislav> 211 #define container_of(ptr, type, member) ({ \ > Borislav> 212 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ > Borislav> 213 (type *)( (char *)__mptr - offsetof(type,member) );}) > > Borislav> I can't understand the ((type *)0) part - type is passed as an argument and it > Borislav> is some struct pointer but the trailing 0 ... what does it actually do? > > No type is passed as an argument as this is a macro. The syntactic You have not read the macro, right? Damn sure a type IS passed as an argument of the macro. In the argument "type". The rest of the explanation is right, though. The overall function of that macro is to compute pointer to a structure from pointer to it's element (provided you know which member of which structure (type) it is). Note, that the list_entry in list.h does exactly the same but shorter. > construct "typeof (expr)" is parsed as a type, the type of the > expression (surprise). For example: > > "int a; typeof (a) b;" - "a" and "b" have the same type, "int" > > "struct S { int x, y; } a; typeof (a.x) b;" - "b" has type "int" > > "((struct S *)0)" is an expression of type "pointer to S", thus > "((struct S *)0)->x" is an expression of type "int", thus > "typeof (((struct S *)0)->x) b" declares an interer variable "b" ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz>
Attachment:
signature.asc
Description: Digital signature