udtya udtya - 1 year ago 74
C Question

Accessing the structure using a pointer

I have declared a structure "node" having a member variable 'm' and then defined two variables as below

struct node t, *p;

Later in the program I assigned the address of

p = &t;

To access the member variable I need to use

But I wanted to use the
operator, but writing it
gives error. Why is it so ?

Answer Source

For this you have to see the precedence of the operators.

The precedence of . operator is higher than * operator.

Writing it like *p.m makes the compiler think that it is *(p.m).

You will have to use (*p).m.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download