title: Trying to understand pointers and double pointers
body: <p>I'm trying to implement a simple linked list in C++. I can create nodes and they seem to link themselves correctly. My question involves the listIterate() function, but I've attached the entire code here in case it is needed. </p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  //Wrapper class which allows for easy list management
  class LinkedList {
  
      //Basic node struct
      struct node {
          int data;
          node *link; 
      }; 
  
      node *head; //Pointer to the head (also referred to as root) node, or the first node created.
      node *current; //Pointer to the /latest/ node, or the node currently being operated on.
      node *tail; //Pointer to the tail node, or the last node in the list. 
  
  public:
  
      //Default constructor. Creates an empty list.
      LinkedList() {
          head = NULL;
          current = NULL;
          tail = NULL;
          cout &lt;&lt; "*** Linked list created. Head is NULL. ***\n";
      }
  
      //Default destructor. Use to remove the entire list from memory.
      ~LinkedList() {
          while(head != NULL) {
              node *n = head-&gt;link;
              delete head;
              head = n;
          }
      }
  
      /*
      appendNode() 
      Appends a new node to the end of the linked list. Set the end flag to true to set the last node to      null, ending the list.
      */
      void appendNode(int i) {
  
          //If there are no nodes in the list, create a new node and point head to this new node.
          if (head == NULL) {
              node *n = new node;
              n-&gt;data = i;
              n-&gt;link = NULL;
              head = n;
  
              //head node initialized, and since it is ALSO the current and tail node (at this point), we must update our pointers
              current = n;
              tail = n;
              cout &lt;&lt; "New node with data (" &lt;&lt; i &lt;&lt; ") created. \n---\n"; 
  
          } else {
  
          //If there are nodes in the list, create a new node with inputted value.
          node *n = new node;
          n-&gt;data = i;
          cout &lt;&lt; "New node with data (" &lt;&lt; i &lt;&lt; ") created. \n"; 
  
          //Now, link the previous node to this node.
          current-&gt;link = n;
          cout &lt;&lt; "Node with value (" &lt;&lt; current-&gt;data &lt;&lt; ") linked to this node with     value (" &lt;&lt; i &lt;&lt; ").  \n---\n";     
  
          //Finally, set our "current" pointer to this newly created node.
          current = n;
          }
      }
  
      /*
      listIterate()
      Iterates through the entire list and prints every element.
      */
      void listIterate() {
  
          //cursor
          node *p;
  
          //Start by printing the head of the list.
          cout &lt;&lt; "Head - Value: (" &lt;&lt; head-&gt;data &lt;&lt; ") | Linked to: (" &lt;&lt; head-&gt;link     &lt;&lt; ") \n";
  
          p = head-&gt;link;
          cout &lt;&lt; *p;
  
      }
  
  }; 
  
  int main() {
  
      LinkedList List;
      List.appendNode(0);
      List.appendNode(10);
      List.appendNode(20);
      List.appendNode(30);
      List.listIterate(); 
  </code></pre>
  
  <p>}</p>
  
  <p><b>Now, </b> I'll refer to this method, listIterate().</p>
  
  <pre><code>void listIterate() {
  
          //cursor
          node *p;
  
          //Start by printing the head of the list.
          cout &lt;&lt; "Head - Value: (" &lt;&lt; head-&gt;data &lt;&lt; ") | Linked to: (" &lt;&lt; head-&gt;link &lt;&lt; ") \n";
  
          p = head-&gt;link; 
          cout &lt;&lt; *p;
  
      }
  </code></pre>
  
  <p>The command <code>cout &lt;&lt; *p;</code> throws an error and I believe this is why: At this point, p is pointing to <code>head-&gt;link</code>, which is another pointer which points to the link field of my head node. Now, I understand if I dereference p at this point in the program, there would be no actual value in <code>head-&gt;link</code> as it points to a variable.  </p>
  
  <p>To me, if I dereference p twice (<code>**p</code>), it should follow the pointer twice (<code>p</code> -> <code>head-&gt;link</code> -> The value of the second node in the linked list (<code>10</code>). However, dereferencing p twice throws this error.</p>
  
  <pre>LinkedListADT.cc:89: error: no match for ‘operator*’ in ‘** p’</pre>
  
  <p>Can anyone help me understand why this is the case? Is this an illegal operation? Is it performed in another way that I'm not familiar with?</p>