title: Using a function to free a linked list with double pointer
body: <p>I am having a tough time deleting all members in a linked in a single function. If I break it up like you see below, it works fine, but this seems wildly inefficient and want to figure out the correct way to do this. in order to free all nodes I need to have function to first free all nodes other then the head, then have a function free the head link. this seems like it would be easy to do but I am having trouble.</p>
  
  <p>Thanks for the help!</p>
  
  <pre><code>int main() {
  
      struct node *head = NULL;
      createList(&amp;head);
  
      //do stuff with list
  
      freeListMembers(head);
      freeListHead(&amp;head);
  
      return 0;
  }
  
  int createList(struct node **head) {
      //create list
      return 0;
  }
  
  void freeListMembers(struct node *head){
      while(head-&gt;next != NULL){
          head-&gt;next = NULL;
          free(head-&gt;next);  
      }
      return;  
  }
  
  void freeListHead(struct node **head) {
      *head = NULL;
      free(*head); 
      return;
  }
  </code></pre>
  
  <p>here is the code that I want to work but does not. the issue I am seeing is a an error for "*head->next;" where it sais "expression must have pointer to struct or union type"</p>
  
  <pre><code>int main() {
  
      struct node *head = NULL;
      createList(&amp;head);
  
      //do stuff with list
  
      freeAllListMembers(&amp;head);
  
      return 0;
  }
  
  int createList(struct node **head) {
      //create list
      return 0;
  }
  
  void freeAllListMembers(struct node **head){ 
      while (head != NULL) {
          struct node *temp = *head-&gt;next;
          free(*head);
          *head = temp ;
      }
     return;  
  }
  </code></pre>