Search code examples
cpointersdouble-pointer

I am using '->' but output is still asking me if I meant to use '->'


I am trying to create the functionality of a Single-Linked List in C, but I am having an issue accessing the next node of my Head node.

typedef struct node {
  struct node *next;
} Node;

int foo(Node **head){
  *head = *head->next;
}

When I run this code, I expect it to change the address of my head node pointer to the next node over, but instead I receive the following error:

‘*head’ is a pointer; did you mean to use ‘->’?
     *head = *head->next;

Solution

  • The line inside foo should be

         *head = (*head)->next
    

    because '->' has higher precedence than *

    You can learn more about operator precedence here (https://en.cppreference.com/w/cpp/language/operator_precedence)