Accessing structure elements using pointers

Posted by Arun Nadesh on Stack Overflow See other posts from Stack Overflow or by Arun Nadesh
Published on 2011-01-06T08:26:06Z Indexed on 2011/01/06 8:53 UTC
Read the original article Hit count: 362

Filed under:
|
|

Hi Everybody,

Greetings!

I got surprised when the following program did not crash.

typedef struct _x{
int a;
char b;
int c;
}x;
main()
{
x *ptr=0;
char *d=&ptr->b;
}

As per my understanding the -> operator has higher precedence over & operator. So I expected the program to crash at the below statement when we try to dereference the NULL pointer tr.

char *d=&ptr->b; 

But the statement &ptr->b evaluates to a valid address. Could somebody please explain where I'm wrong?

Thanks & Regards, Arun

© Stack Overflow or respective owner

Related posts about c

    Related posts about pointers