Is apparent NULL pointer dereference in C actually pointer arithmetic?

Posted by karthik A on Stack Overflow See other posts from Stack Overflow or by karthik A
Published on 2010-12-26T04:48:59Z Indexed on 2011/01/01 21:53 UTC
Read the original article Hit count: 141

Filed under:
|
|

hey ive got this piece of code. It dereferences a null pointer here. But then there is an and with unsigned int. I really dont understand the whole part. Can someone explain the output.??

struct hi  
{
   long a;  
   int b;  
   long c;  
};  

int main()  
{  
    struct hi ob={3,4,5};  
    struct hi *ptr=&ob;  
    int num= (unsigned int) & (((struct hi *)0)->b);  

   printf("%d",num);  
   printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b)));  
}  

The output I get is 44. But how does it work?

© Stack Overflow or respective owner

Related posts about c

    Related posts about pointers