title: C linked Lists/-> Operator
body: <p>So I've been trying to understand the concepts of linked lists (Been looking at some example code, I found this one on the internet. Now if I could kindly have someone confirm if I have grasped some of the concepts correctly. I will draw diagrams of what I think each linke of code does.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
     struct ListItem {
     int data;
     struct ListItem *next;
  };
  
  int main (int argc, char *argv[]){
  
     struct ListItem a;  
     a.data = 0;
     a.next = NULL;
     struct ListItem b;
     b.data = 1;
     b.next = NULL;
     struct ListItem c;
     c.data = 2;
     c.next = NULL;
  
     a.next = &amp;b;
     b.next = &amp;c;
  
     int counter = 0;
     struct ListItem *i;
     for (i = &amp;a; i != NULL; i = i-&gt;next){
  
        printf("Item %d value is %d\n\r", counter, i-&gt;data);
        counter++;
     }
  
  
     return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>Snippet of code 1:</p>
  
  <pre><code>struct ListItem {
      int data;
      struct ListItem *next;
  };
  </code></pre>
  
  <p>This creates a structure called ListItems. There are two components to the structure, a component to store the data and a pointer to another structure of type struct ListItem. I visualise the linked list like this: </p>
  
  <p><img src="https://i.stack.imgur.com/PMGTc.png" alt="enter image description here"></p>
  
  <p>Is this a correct way of visualising it?</p>
  
  <hr>
  
  <p>Code snippet 2: </p>
  
  <pre><code>struct ListItem a;  
  a.data = 0;
  a.next = NULL;
  struct ListItem b;
  b.data = 1;
  b.next = NULL;
  struct ListItem c;
  c.data = 2;
  c.next = NULL;
  </code></pre>
  
  <p>Yup, I know it can be cut shorter but I just did it this way to see if I can understand the concept. Now this snippet creates a variable "a", "b" and "c" of type struct ListItem. Then it sets the first member (data) of each structure to 0, 1, 2 respectively and the second member (next) to point to NULL. So my visualisation now is like this:</p>
  
  <p><img src="https://i.stack.imgur.com/aLx0K.png" alt="enter image description here"></p>
  
  <p>Now <strong>MORE QUESTIONS</strong>:</p>
  
  <p><strong>Question 1:</strong> When we are initially the pointer to NULL, it points to nothing correct? Why do we do this? Wasn't it originally pointed to nothing?</p>
  
  <hr>
  
  <p>Snippet 3:</p>
  
  <pre><code>   a.next = &amp;b;
     b.next = &amp;c;
  </code></pre>
  
  <p>This let's next in each variable a, b (which is a structure) point to the address memory location of b and c respectively.</p>
  
  <p>My visualisation: <img src="https://i.stack.imgur.com/5yFTj.png" alt="enter image description here"></p>
  
  <p><strong>Question</strong>: How can it do this? Isn't the structure itself stored over multiple memory address (4 for int, etc)</p>
  
  <hr>
  
  <p>Snippet 4: </p>
  
  <pre><code>   int counter = 0;
     struct ListItem *i;
     for (i = &amp;a; i != NULL; i = i-&gt;next){
  
        printf("Item %d value is %d\n\r", counter, i-&gt;data);
        counter++;
     }
  </code></pre>
  
  <p>Here is the snippet I've been a bit confused with. Now, we set aside an integer called counter and initailise this to zero. Also, we create a variable called i that points to the type struct ListItem. Now could someone explain to me the for loop? I am a bit confused on what it's doing. In particular, i=i->next, I'm not familiar with this. I know it's equivalent to i=(*i).next but not sure what it really does. Could someone create a quick diagram?</p>
  
  <p><strong>ALSO</strong>: If anyone has any good resources/links (no pun intended) to some useful websites to help me understand linked lists a bit better feel free to post them.</p>