[Ilugc] C doubt involving pointers

  • From: p_mdriyaz@xxxxxxxxxxx (Mohammed Riyaz)
  • Date: Wed May 18 15:11:19 2005

Hi,

On Wed, 18 May 2005 04:55:02 -0400, "Binand Sethumadhavan"
<binand@xxxxxxxxx> said:

On 18/05/05, Mohammed Riyaz <p_mdriyaz@xxxxxxxxxxx> wrote:
Yeah i understand that, but i dont understand the #define statement
syntax wise. like what is (char *)&((type *)0)->mem. and how does it
work?

#define offsetof(type, mem) ((size_t) \
                       ((char *)&((type *)0)->mem - (char *)(type *)0))



You could very well have written the above macro as:

size_t offset = fubar->member - fubar; /* disregarding all the casts */

and gotten the same result. 


Well, this makes perfect sense. But,


((type *)0)->mem - (type *)0;


dosent make any sense to me. isint (type *)0 a null pointer??  so how is
it possible to offset mem, when we dont even have a structure there??

Thanks,
Mohammed Riyaz P.


Other related posts: