title: My linked list results in segmentation fault every time even though I cannot see a flaw
body: <p>I've been learning programming for some while now and I've seen this concept of &quot;linked list&quot; a few times and decided to give it a try:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct cell {
      int number;
      struct cell* next;
  } cell;
  
  typedef struct {
      cell* head;
  } cell_list;
  
  
  void push_front(cell_list* list, int number) {
      printf(&quot;here\n&quot;);
      cell* n;
      printf(&quot;here2\n&quot;);
      n-&gt;number = number;
      printf(&quot;here3\n&quot;);
      n-&gt;next = list-&gt;head;
      printf(&quot;here4\n&quot;);
      list-&gt;head = n;
  }
  
  int main(int argc, char* argv[]) {
      cell_list* L;
  
      push_front(L, 5);
      push_front(L, 8);
      push_front(L, 19);
  
      cell* n = L-&gt;head;
  
      // this should print &quot;19 8 5&quot;
      while(n != NULL) {
          printf(&quot;Val: %d\n&quot;, n-&gt;number);
          n = n-&gt;next;
      }
  
      return 0;
  }
  </code></pre>
  <p>output:</p>
  <pre><code>here
  here2
  here3
  Segmentation fault: 11
  </code></pre>
  <p>I am looking for an answer to the following two questions:
  (1) What is the correct way of doing this
  (2) What is actually happening in my example? What is going wrong?</p>
  <p>I am thinking that it is the compiler that fails to assign &quot;struct cell* next&quot; to be equal to &quot;cell* head&quot;, etc.) OR that the FIRST allocated memory for head and the fist cell with the next property is at fault here.</p>
  <p>Also similar posts and questions have answer to questions identical to mine however they fail at multiple points:
  (1) the code example posted by OP is overly complex
  (2) the code answers are good but... check 3
  (3) the code answers and not explained, just &quot;omg just use malloc you are casting voids everywhere&quot;
  (4) 1 - 3 results in a complete post with Q&amp;A that is above my level. If your answer is &quot;use malloc&quot; or &quot;the pointers point at random memory&quot; or &quot;you are pointing at null&quot;. Please explain because these are just programming jargon/language sentences that don't actually go into depth so that I, a beginner, can understand.</p>
  <p>I am fully aware there are identical posts but none truly explain to a beginner like me why they allocate memory will malloc and cast the allocated space to become a linked list struct. If your answer is the same as in any of those posts, then please explain why this way is faulty and not just &quot;This is how you do, there are 12893 posts about malloc and linked lists.&quot; I understand malloc and casting if that was not obvious but not how they properly fit in here and how my current program is created on the stack.</p>