This macro is used to find the address of a struct given one of its member.
So, for example, suppose you have the struct:
typedef struct
{
int i;
int j;
} typestruct;
First thing you need to know is that the last part of the macro:
&((typestruct *)0)->j
Is used to give the offset of a member. So, it is the size, in bytes, from the zero memory casted to the type, to the member. In this case, it is the sizeof(int)
, because j
is just bellow int i
; So lets assume this expression values 4
for simplicity. You can get the same result with the macro
offsetof(typestruct, j);
Now we want to calculate the address of temp
, where temp
is typestruct temp
. To do that, we simple compute the address of the pointer minus the member position. The address of the pointer is:
(typestruct *)((char *) &temp.j)
Hence, the subtraction is:
&temp == (typestruct *)((char *) &temp.j) - offsetof(typestruct, j)
or, like the macro says:
&temp == (typestruct *)((char *) &temp.j) - &((typestruct *)0)->j
You can learn much more here, and also in this question.
(Parenthesis are necessary, but was eliminated for clarification)