c++ node list - NULL test not working
<p>I wanted to test the following code (which works fine for a non-null list) to see what would happen in the case of an empty list (in which case the head would be null).</p>

<p>hence the code which applies to filling the list is commented out..</p>

<p>But for some strange reason, the test for NULL in print_nodes() just doesnt seem to work. ive added some debug cout calls to see (and also checked using gdb) but whilst the value does indeed appear to be NULL, any if statements dont seem to test the equivalence properly..</p>

<p>any idea why?</p>

<p>many thanks!</p>

<pre><code>   #include &lt;iostream&gt;
using namespace std;

struct node {
  char dat;
  node *nextPtr;
};

//inserts new node and returns pointer
node* new_node(char data, node* prevNode);

//adds a new node at the head ofthe list
void new_head (node *head_, char dat_);

//inserts new node after *before
void insert_node (node *before, char dat_);

//runs through and prints the list - requires first node (head)
void print_nodes (node *head);


int main() {
  cout &lt;&lt;endl &lt;&lt; endl;
  cout &lt;&lt; endl &lt;&lt; "*******************RUN******************" &lt;&lt;endl &lt;&lt;endl;

  node* head = NULL;

  if (head == NULL) { 
    cout &lt;&lt; "head null"; //this works here
  }


  //head non-standard
  //  node* head = new node;
  //  head-&gt;dat ='a';

  /*
  node* b = new_node('b', head);
  node* c = new_node('c', b);
  node* d = new_node('d', c);
  node* e = new_node('e', d);
  node* f = new_node('f', e);

  */
  print_nodes(head);

  insert_node(head,'N');

  print_nodes(head);

  cout &lt;&lt; endl &lt;&lt; "*******************END RUN******************" &lt;&lt;endl;
  return 0;
}

node* new_node(char data, node* prevNode) {
  node* tempPtr = new node;
  tempPtr-&gt;dat = data;
  tempPtr-&gt;nextPtr = NULL; //standard
  prevNode-&gt;nextPtr = tempPtr;
  return tempPtr;
}

void new_head (node *head_, char dat_) {

}

void insert_node (node *before, char dat_) {
  node* tempPtr = new node;
  tempPtr-&gt;dat = dat_;

  tempPtr-&gt;nextPtr = before-&gt;nextPtr;
  before-&gt;nextPtr = tempPtr;

}


void print_nodes (node *head) {

  node* tempPtr = head;

  cout &lt;&lt; "\nPrinting nodes..." &lt;&lt;endl;

  if (tempPtr == NULL) { //this test is not working.. why?
    cout &lt;&lt; "tempPtr is NULL";
    return; 
  } else { //only run in the non null case
    for (tempPtr; tempPtr != NULL; tempPtr = tempPtr-&gt;nextPtr) {
      cout &lt;&lt; "Current node content: " &lt;&lt; tempPtr-&gt;dat &lt;&lt;endl;
    }  
  }
}
</code></pre>
