Order of evaluation of pointer dereference operator

181 views Asked by At

I was writing some code for deleting the node from the doubly linked list and I came across a following syntax. Though I am able to understand what the syntax is doing but not able top get How is it possible below is the code

struct dll{
   int data;
   struct dll *next;
   struct dll *prev;
};

and in some function in there is this syntax

else{
  temp2=temp->prev;
  temp2->next=temp->next;
  temp->next->prev=temp2;
  free(temp);
}

I know what temp->next it makes the temp pointer point to the next node and from the syntax I could also understand that temp->next->prev will make the pointer point to the prev pointer of the next node.

But the question is how this syntax is evaluated ?

1

There are 1 answers

1
Roddy On

I think you've misunderstood what the -> operator does. It doesn't make temp point anywhere different, it just accesses a member from the structure pointed to by the first operand.

https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

The syntax is evaluated left to right, and operator precedence means that the structure dereference happens before the assignment. So the third line (for example) is really this:-

((temp->next)->prev) = temp2;