0

Possible Duplicate:
Why subtract null pointer in offsetof()?

I hava a question when i read the source code of apache.

in the apr_general.h, there exists a definition as shown below:

#define APR_OFFSET(p_type,field) \
    ((long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))

#endif /* !CRAY */

i am juse confused why we should minus the ((char *) NULL)) in this op.

is there anyon that can help me?

Community
  • 1
  • 1

1 Answers1

0

This trick is used to get an offset (an integer, not pointer) from the beginning of structure

Serge
  • 6,088
  • 17
  • 27
  • ups. too fast. to get an offset of a field from the beginning of structure – Serge Sep 20 '12 at 19:42
  • Thank you for you reply. But maybe I didn't make myself clear. I mean (char *) (&(((p_type)NULL)->field))) here has got the offset of the field in the struct p_type, but why minus ((char *) NULL)) here. – user1687017 Sep 21 '12 at 02:03
  • (char*)(....) is a pointer to char, not an integer. if you subtract from this value a (char*)(NULL) then you convert it to the integer value. that's it – Serge Sep 21 '12 at 02:08