title: Printing Linked List leads to strings with garbage values (C)
body: <p>I was reading a linked list from a .txt file to fill the following node:</p>
  <pre><code>typedef struct node {
      
      int data;
      char* line;
      struct node* next;
  
  } NODE;
  </code></pre>
  <p>The code works fine and reads the file into the linked list. I print out the string while each node is being initialized. However whenever I call the <code>printList()</code> function, the <code>line</code> string is always a garbage value. Sample run:</p>
  <pre><code>./a.out linkedlisttext.txt
  
  data in readFile(): 4
  string in readFile():  Hello world
  address in readFile(): 0x1326067a0
  
  
  data in readFile(): 28
  string in readFile():  What is up world???
  address in readFile(): 0x1326067c0
  
  
  data in readFile(): 2124
  string in readFile():  your mother
  address in readFile(): 0x1326067e0
  
  
  data in readFile(): 85
  string in readFile():  more data
  address in readFile(): 0x132606800
  
  
  data in readFile(): 9421
  string in readFile():  just a little more data
  address in readFile(): 0x132606820
  
  
  data in readFile(): 992
  string in readFile():  are we almost there?
  address in readFile(): 0x132606840
  
  
  data in readFile(): 301
  string in readFile():  we did it!
  address in readFile(): 0x132606860
  
  DATA:    4 | STRING �g`2
  address in printList(): 0x1326067a0
  DATA:   28 | STRING �g`2
  address in printList(): 0x1326067c0
  DATA: 2124 | STRING �g`2
  address in printList(): 0x1326067e0
  DATA:   85 | STRING h`2
  address in printList(): 0x132606800
  DATA: 9421 | STRING 0h`2
  address in printList(): 0x132606820
  DATA:  992 | STRING Ph`2
  address in printList(): 0x132606840
  DATA:  301 | STRING 
  address in printList(): 0x132606860
  </code></pre>
  <p>Here is <code>linkedlisttext.txt</code></p>
  <pre><code>4, Hello world
  28, What is up world???
  2124, your mother
  85, more data
  9421, just a little more data
  992, are we almost there?
  301, we did it!
  </code></pre>
  <p>and here is the code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  typedef struct node {
      
      int data;
      char* line;
      struct node* next;
  
  } NODE;
  
  
  struct node* readFile(FILE* fp, NODE* root) {
      char line[100];
      char* token;
      NODE* curr;
      NODE* prev;
  
      
      while(fgets(line, 100, fp) != NULL) {   
          
          curr = malloc(sizeof(struct node*));
  
          token = strtok(line, &quot;,&quot;);
          
          curr-&gt;data = atoi(token);
  
          printf(&quot;\ndata in readFile(): %d\n&quot;, curr-&gt;data);
          
          token = strtok(NULL, &quot;&quot;);
          curr-&gt;line = (char*) malloc(sizeof(token)  + 1);
  
          strcpy(curr-&gt;line, token);
          printf(&quot;string in readFile(): %s&quot;, curr-&gt;line);
          printf(&quot;address in readFile(): %p\n\n&quot;, curr-&gt;line);
  
  
          curr-&gt;next = NULL;  
      
          
          if(root == NULL) {
              root = curr;
          } else {
              NODE* travel = root;
  
              while(travel-&gt;next != NULL) {
                  travel = travel-&gt;next;
              }
  
              travel-&gt;next = curr;
  
          }
  
          
      }
  
      return root;    
  
  }
  
  void printList(NODE* root) {
      
      struct node* temp = root;
  
      while(temp-&gt;next != NULL) {
          printf(&quot;DATA: %4d | STRING %s\n&quot;, temp-&gt;data, temp-&gt;line);
          printf(&quot;address in printList(): %p\n&quot;, temp-&gt;line);
  
          
          temp = temp-&gt;next;
      }
  
      printf(&quot;DATA: %4d | STRING %s\n&quot;, temp-&gt;data, temp-&gt;line);
      printf(&quot;address in printList(): %p\n&quot;, temp-&gt;line);
  
  }
  
  int main(int argc, char** argv) {
      
      FILE* fp = fopen(argv[1], &quot;r&quot;);
      NODE* root = NULL;  
  
      if(fp == NULL) {
          return 0;
      }   
  
      root = readFile(fp, root);
      printList(root);
      
  }
  </code></pre>
  <p>Any help is appreciated, thanks!</p>