title: malloc and pointer in a struct
body: <p>I have the following C code:</p>
  
  <pre><code>typedef struct DListNode_ {
      void    *data;
      struct DListNode_ *prev;
      struct DListNode_ *next;
  } DListNode;
  
  
  typedef struct DList_ {
      int size;
      DListNode  *tail;
      DListNode  *head;
  } DList;
  
  void insert(DList * list, DListNode * element, int data) {
      DListNode * new_element = (DListNode *)malloc(sizeof(DListNode));
      new_element-&gt;data = &amp;data;
      if (list-&gt;head==NULL) {
          list-&gt;head=list-&gt;tail=new_element;
          list-&gt;size++;
          return;
      }
      if(element == NULL) {
          // handle size==0?
          new_element-&gt;next=list-&gt;head;
          list-&gt;head-&gt;prev=new_element;
          list-&gt;head=new_element;
          list-&gt;size++;
      } else {
          printf("Not yet implemented!\n");
      }
  }
  
  void printNodes(DList *list) {
      DListNode * pointer = list-&gt;head;
      if (pointer!=NULL) {
          int v= *((int*)pointer-&gt;data);
          printf("Node has value: %d\n", v);
          while (pointer-&gt;next != NULL) {
              v = *((int*)pointer-&gt;data);
              printf("Node has value: %d\n", v);
              pointer=pointer-&gt;next;
          }
      }
  }
  
  int main(int argc, const char * argv[])
  {
  
      int e0 = 23;
      int e1 = 7;
      int e2 = 11;
      DList *list = (DList *)malloc(sizeof(DList));
      initList(list);
      assert(count(list)==0);
      insert(list, NULL, e0);
      assert(count(list)==1);
  
      insert(list,NULL, e1);
      assert(count(list)==2);
  
      insert(list,NULL, e2);
      assert(count(list)==3);
      printNodes(list);
  
      return 0;
  }
  </code></pre>
  
  <p>I have a few problems:</p>
  
  <ol>
  <li>does <strong>DListNode * new_element = (DListNode *)malloc(sizeof(DListNode));</strong> also allocate space for the, data, prev, next pointer or do I manually need to call malloc on each of those pointers?</li>
  <li>When I print the content of the data pointer in each node they all have the value <strong>3</strong> even though I insert 23, 7 and 11 and set the data pointer to the address of the int: **   new_element->data = &data;**.</li>
  </ol>
  
  <p>(Introductionary textbooks on C have been ordered)</p>
  
  <p>EDIT:</p>
  
  <p>insert now takes a void pointer to the data:</p>
  
  <pre><code>// Insert data as the new head
  void insert(DList *list, DListNode *element, void *data) {
      DListNode *new_element = malloc(sizeof(DListNode));
      new_element-&gt;data = data;
      if (list-&gt;head==NULL) {
          list-&gt;head=list-&gt;tail=new_element;
          list-&gt;size++;
          return;
      }
      if(element == NULL) {
          new_element-&gt;next=list-&gt;head;
          list-&gt;head-&gt;prev=new_element;
          list-&gt;head=new_element;
          list-&gt;size++;
      } else {
          printf("Not yet implemented!\n");
      }
  }
  </code></pre>
  
  <p>In main I do:</p>
  
  <pre><code>int main(int argc, const char * argv[])
  {
      int i0=7;
      int *ip0 = malloc(sizeof(int));
      ip0 = &amp;i0;
  
      int i1=8;
      int *ip1 = malloc(sizeof(int));
      ip1 = &amp;i1;
  
      int *ip2 = malloc(sizeof(int));
      int i2=44;
      ip2 = &amp;i2;
  
      DList *list = malloc(sizeof(DList));
      initList(list);
      // create some nodes
      assert(count(list)==0);
      insert(list, NULL, ip0);
      assert(count(list)==1);
  
      insert(list,NULL, ip1);
      assert(count(list)==2);
  
      insert(list,NULL, ip2);
      assert(count(list)==3);
      printNodes(list);
  
      return 0;
  }
  </code></pre>
  
  <p>which outputs:</p>
  
  <pre><code>Node has value: 44
  Node has value: 44
  Node has value: 8
  </code></pre>
  
  <p>but it should be:</p>
  
  <pre><code>Node has value: 44
  Node has value: 8
  Node has value: 7
  </code></pre>