title: Segmentation Fault 11, C
body: <p>I've been trying to implement a <code>LinkedList</code> in C to practically implement the theory I have learnt in C. I keep getting a <code>Segmentation fault: 11.</code> Any help on how to fix this issue would be highly appreciated. Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct Element {
      int value;
      struct Element *nextElement;
  };
  
  struct LinkedList {
      struct Element *firstElement;   
  };
  
  void add(struct LinkedList *list, int value) {
      if(list-&gt;firstElement == NULL) {
          struct Element *newElement;
          newElement = malloc(sizeof(struct Element));
  
          newElement-&gt;value = value;
  
          list-&gt;firstElement = newElement;
      } else {
          struct Element *lastElement;
  
          lastElement = list-&gt;firstElement;
  
          while(lastElement-&gt;nextElement != NULL) {
              lastElement = lastElement-&gt;nextElement;   
          }
  
          struct Element *newElement;
          newElement = malloc(sizeof(struct Element));
  
          newElement-&gt;value = value;
  
          lastElement-&gt;nextElement = newElement;
      }
  }
  
  void showAllElements(struct LinkedList *list) {
      struct Element *lastElement = list-&gt;firstElement;
  
      while(lastElement != NULL) {
          printf("%d\n", lastElement-&gt;value);
          lastElement = lastElement-&gt;nextElement;
      }
  }
  
  int main()
  {
      struct LinkedList list;
      add(&amp;list, 5);
      add(&amp;list, 10);
      add(&amp;list, 15);
      showAllElements(&amp;list);
  
      return 0;   
  }
  </code></pre>