title: Creating a linked list with a for loop
body: <p>Here is my struct </p>
  
  <pre><code>struct ListItem{
  
      int data;
      struct ListItem *next;
  
  };
  </code></pre>
  
  <p>Assuming the first node of the linked list will have data = 0, I want to write a for loop that creates a linked list of size 5 but I'm not sure how to work</p>
  
  <p>I tried the following</p>
  
  <pre><code>int main(int argc, char* argv[]){
  
      struct ListItem a;
      a.data = 0;
  
  
      for (int i = 1; i &lt; 5; i++){
          struct ListItem *pointer = &amp;a;
          struct ListItem nextnode;
          nextnode.data = i;
          a.next = &amp;nextnode;
          pointer = pointer-&gt;next;
  
      }
  }
  </code></pre>
  
  <p>But the result is
  a.data = 0
  and a.next->data = 4</p>