title: C - Linked List and pointer problem
body: <p>Hey,<br>
  I'm a beginner in C and tried to implement my own linked list implementation, that basically looks like this:</p>
  
  <pre><code>struct Element
  {
      void *value;
      struct Element *next;
  };
  
  typedef struct
  {
      struct Element *first;
      struct Element *last;
      unsigned int size;
  } LinkedList;
  
  void LinkedList_init(LinkedList *this)
  {
      this-&gt;size = 0;
      this-&gt;first = NULL;
      this-&gt;last = NULL;
  }
  
  void LinkedList_add(LinkedList *this, void *value)
  {
      struct Element *node = malloc(sizeof(struct Element));
      node-&gt;value = value;
      node-&gt;next = NULL;
  
      if (this-&gt;size == 0)
          this-&gt;first = this-&gt;last = node;
      else
      {
          this-&gt;last-&gt;next = node;
          this-&gt;last = node;
      }
  
      this-&gt;size++;
  }
  </code></pre>
  
  <p>So in short, I want a linked list that can hold arbitrary types - I heard, this is possible in C by using void pointers.
  The problem now arises, when I want to use that implementation, for example with a structure as value:</p>
  
  <pre><code>typedef struct
  {
      int baz;
  } Foo;
  
  int main(void)
  {
      LinkedList list;
      Foo bar;
      bar.baz = 10;
  
      LinkedList_init(&amp;list);
      LinkedList_add(&amp;list, (void *) &amp;bar);
  
      /* try to get the element, that was just added ... */
      Foo *firstElement = (Foo *)list.first;
      /* ... and print its baz value */
      printf("%d\n", firstElement-&gt;baz);
  
      return 0;
  }
  </code></pre>
  
  <p>The last printf call just prints values like -1077927056, which look like a memory address. So it's probably a problem with pointers. After searching the web the last few days for a similar issue on the web (I had no luck with that), I tried to throw my own logic away and tested various random *&amp; combinations. Turns out, that was a dead end, too. :(</p>
  
  <p>It's probably something simple for a more experienced C programmer, but I just can't find the answer. Please help :D</p>