Accepting another structure member via a pointer


int main()
    struct node
        int data;
        struct node *next;


    struct node n1,n2,n3;
    int i; = 100; = 200; = 300; = &n2; = &n3;

    i = n1.(*next).data;


    return 0;


My question is simple. Why can't I access n2 node via n1 node, when I use this i = n1.(*next).data;

one I get an error. However, if I change it>data

I thought (*x).y

u x->y

meant the same thing.


source to share

2 answers

You are right, (*x).y

and x->y

mean the same thing: why should you write (*

(which is equivalent to>data


is yours x


EDIT: The parentheses are that the order of evaluation is:

  • Take n1: n1

    (a struct node

  • Take the next term: (n1).next

    (a struct node *

  • Mark pointer: *((n1).next)

    (a struct node

  • Take item data: (*((n1).next)).data

    (a int


Read what the C11 standard says in @ Christophe's answer, and notice how the operator .

used in steps 2 and 4 above is used with struct node

as the first operands (see steps 1 and 3) and the member names as second operands.



The syntax requires i = (*



  • The C11 standard says: "The first operand of an operator must be an atomic, qualified or unqualified structure or union type, and the second operand must name a member of that type." (§ In this way, n1.*next

    and n1.*(next)

    they are not legitimate, because the operator .

    can follow only a member name .


    is of type "pointer to struct node", you can dereference it using the *

    or operator ->


  • Through dereference, it *

    will be a value of type "struct node". Unfortunately, the prefix operator *

    has a lower precedence than .

    . This is why *

    means *(

    , which of course is not valid because it

    is a pointer and a pointer itself has no members. But it (*

    is of type "struct node", so it (*

    correctly refers to the member value.

  • This kind of break is quite common, and therefore exists ->

    . The standard says: "The first operand of the → operator must have a pointer of type" "to an atomic, qualified, or unqualified structure (...), and the second operand must name the member of the type it points to." This explains>data

    . Fortunately, the priority .

    and ->

    the same, and they are evaluated from left to right, so no parentheses are required.



All Articles