title: variable initialization using class member function
body: <p>Why the head is not NULL at the beginning if I initialized it with the class member function? It is a tricky part.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct node
  {
      int data;
      node *next;
  };
  
  
  class linked_list
  {
  private:
      node *head;
  public:
  
      void push(node *head, int data);
      void printList(node *head);
      void printMiddle(node *head);
      void makeHeadNull(node *head);
  };
  
  void linked_list::push(node *head, int data)
  {
      node *newNode=new node;
      if(head==NULL)
      {
          head=newNode;
      }
      else {
          newNode-&gt;data = data;
          newNode-&gt;next = head;
          head = newNode;
      }
  }
  
  
  
  void linked_list::printMiddle(node *head)
  {
  
          node *slow_ptr = head;
          node *fast_ptr = head;
  
          if (head!=NULL)
          {
              while (fast_ptr != NULL &amp;&amp; fast_ptr-&gt;next != NULL)
              {
                  fast_ptr = fast_ptr-&gt;next-&gt;next;
                  slow_ptr = slow_ptr-&gt;next;
              }
              cout&lt;&lt;slow_ptr-&gt;data&lt;&lt;" ";
      }
  
  
  
  void linked_list::printList(node *head)
  {
      while(head!=NULL)
      {
          cout&lt;&lt;head-&gt;data&lt;&lt;" ";
          head=head-&gt;next;
  
      }
  }
  
  void linked_list::makeHeadNull(node *head)
  {
      head=NULL;
  }
  
  
  
  int main()
  {
      node *head;
      linked_list l;
      l.makeHeadNull(head);
  
  
      l.push(head, 0);
      l.push(head, 1);
      l.push(head, 2);
      l.push(head, 3);
      l.push(head, 4);
  
      l.printMiddle(head);
  }
  </code></pre>
  
  <p>I wanted to initialize head inside a constructor, but when I initialized head like that, the head node has always been NULL inside main. When I initialize head in the constructor and then I want to pass it as a parameter of a function push I get errors.</p>