udtya udtya - 15 days ago 6
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
t
to
p
:

p = &t;


To access the member variable I need to use
p->m
.

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

Answer

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.

Comments