I came across a piece of code where NULL is typecast to an structure pointer type (foo *) 0
,and with that pointer de-referencing a member ((foo *)0)->m)
,and using address of that &(((foo *)0)->m))
and type casting it to integer to get the memory index of that member with in the structure.((unsigned int)(&(((foo *)0)->m)))
.
I also see that this macro has specific behavior of giving the offset all the times by giving the index.Please try to help enlighten me.Thanks for the inputs.
#include <stdio.h>
#define MACRO(m) ((unsigned int)(&(((foo *)0)->m)))
typedef struct
{
int a;
int b;
int c;
int d;
}foo;
int main(void) {
printf("\n %d ", MACRO(c));
return 0;
}