Search code examples
c++pointersdereference

Why parenthesis are needed when dereferencing a double pointer?


I have this function to delete the very first node in a single linked list:

void removeFront(Node **tmpHead){       
if ((*tmpHead)->next == NULL)
    cout << "Single Node! RemoveFront() aborted!\n";'
else{
Node *oldNode = *tmpHead;
*tmpHead = oldNode->next;
delete oldNode;     
}

}

Why do I need to put *tmpHead between brackets in the if statement? A compilation error is given if I don't.


Solution

  • Due to operator precedence, *tmpHead->next is interpreted as *(tmpHead->next).

    Since tmpHead is of type Node**, tmpHead->next is not a valid sub-expression.

    That's why you need to use parenthesis around *tmpHead and use (*tmpHead)->next == NULL.