title: Error Unhandled exception at 0x00D0345E in linkedlist.exe: 0xC0000005: Access violation writing location 0x00000004
body: <p>I try create linked-list with n element in c++.</p>
  
  <p>But I received an error message:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x00D0345E in linkedlist.exe: 0xC0000005: Access violation writing location 0x00000004.</p>
  </blockquote>
  
  <p>What is the reason?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct linklist
  {
      int data;
      linklist *next;
  };
  
  void Create_linklist(linklist &amp;l,int n) // with n element
  {
      linklist *phead,*p;
      int i=0;
      phead=p=NULL;
      while (i&lt;n)
      {
          if (phead==NULL)
          {
              phead=new linklist;
              p=phead;
          }
          else
          {
              p-&gt;next=new linklist; //--&gt; Seem error this line
              p=p-&gt;next;
          }
          cout&lt;&lt;"Input value "&lt;&lt;i+1&lt;&lt;": ";
          cin&gt;&gt;p-&gt;data;
          i++;
          p=p-&gt;next=NULL;
      }
  }
  
  void Outer_list(linklist l)
  {
      linklist *p=new linklist;
      if (p!=NULL)
      {
          cout&lt;&lt;p-&gt;data&lt;&lt;" ";
          p=p-&gt;next;
      }
  }
  void main()
  {
      linklist list;
      Create_linklist(list,5);
      Outer_list(list);
      system("pause");
  }
  </code></pre>