title: C - Appending new data to linked list changes the previous data, too
body: <p>I am new to C and have stumbled upon a problem I hope you can help me with. </p>
  
  <p>We have a double linked list</p>
  
  <pre><code>struct LinkedList {
      LinkedListNode *first;
      LinkedListNode *last;
  };
  </code></pre>
  
  <p>and the nodes which are elements therein</p>
  
  <pre><code>struct LinkedListNode {
      LinkedListNode *previous;
      LinkedListNode *next;
      char *data;
  };
  </code></pre>
  
  <p>Trying to append elements to the list, I wrote a function which creates a node at the end of the list with the given data. </p>
  
  <pre><code>void append(LinkedList *list, char *data)
  </code></pre>
  
  <p>It works properly if I call the function like this:</p>
  
  <pre><code>append(list, "abc");
  append(list, "def");
  </code></pre>
  
  <p>Which leads to the linked list containing 2 nodes - the first one with data being <code>"abc"</code> and the second one with data being <code>"def"</code>.</p>
  
  <p>However, if I pass the data as a variable, for example with</p>
  
  <pre><code>char testData[10] = "";
  strcpy(testData, "abc");
  append(list, testData);
  strcpy(testData, "def");
  append(list, testData);
  </code></pre>
  
  <p>The result is 2 linked list nodes both with the data <code>"def"</code>. </p>
  
  <p>The reason for it probably is that the data pointer inside the first and second node both point to the same memory, however, I don't know how to change this fact or how to circumvent it.</p>
  
  <p>The code where I pass the data inside the function looks like this:</p>
  
  <pre><code>struct LinkedListNode *newNode = malloc(sizeof(struct LinkedListNode));
  newNode-&gt;data = data;
  </code></pre>
  
  <p>I hope you can help me and thanks in advance.</p>