offsetof macro and container_of
Offsetof macro action: calculating an offset relative to the first address of an element in the structure structure
The role of macro container_of: Pointer know the structure of a member variable, pointer thrust reverser structure variables
#include <stdio.h> #define the offsetof (type, Member) ((int) & ((type *) 0) -> Member) #define container_of (PTR, type, Member) ({\ const typeof (((type *) 0 ) -> Member) * the mptr = (PTR); \ (type *) (( char *) the mptr - the offsetof (type, Member));}) // action typeof keyword: typeof (a) by the obtaining a variable a variable type typedef struct Node { int a; Double B; a float C; } N1; int main () { N1 S; a float * P = & (SC); the printf ("offsetof(c) = %d\n",offsetof(n1,c)); printf("addr of s is %p\n",&s); printf("container_of(p,n1,c) = %p\n",container_of(p,n1,c)); return 0; }
the sizeof ( Short ) = 2 the sizeof ( int ) = . 4 the sizeof ( Long ) = . 4 -------------------------------- the After the Exited Process 0.1803 seconds The with return value 0 press any key to continue...