title: C linked list program won't display a string
body: <p>I have a problem in a homework where I work with linked list.
  The problem is that not all datas are displayed, when I try to printf them.
  This is my input:</p>
  
  <pre><code>insert JablkaJonagold AppleVillage 10 0.35 kg
  display
  </code></pre>
  
  <p>This is my output:</p>
  
  <pre><code>Name=, Manufacturer=AppleVillage, Quantity=10.000000, Price=0.350000, MJ=kg
  </code></pre>
  
  <p>But my output should look like this:</p>
  
  <pre><code>Name=JablkaJonagold, Manufacturer=AppleVillage, Quantity=10.000000, Price=0.350000, MJ=kg
  </code></pre>
  
  <p>I tried many compillers, and It's running without errors, expect on rextester.com, which gave me this error:</p>
  
  <pre><code>*** buffer overflow detected ***: /var/www/service/usercode/973543283/a.out terminated
  ======= Backtrace: =========
  /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ff45289b7e5]
  /lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x5c)[0x7ff45293c56c]
  /lib/x86_64-linux-gnu/libc.so.6(+0x116570)[0x7ff45293a570]
  /lib/x86_64-linux-gnu/libc.so.6(+0x1158c2)[0x7ff4529398c2]
  /var/www/service/usercode/973543283/a.out[0x4008d9]
  /var/www/service/usercode/973543283/a.out[0x4006b9]
  /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7ff452844830]
  /var/www/service/usercode/973543283/a.out[0x400739]
  ======= Memory map: ========
  00400000-00401000 r-xp 00000000 b6:4f3a1 2919345                         /var/www/service/usercode/973543283/a.out
  00600000-00601000 r--p 00000000 b6:4f3a1 2919345                         /var/www/service/usercode/973543283/a.out
  00601000-00602000 rw-p 00001000 b6:4f3a1 2919345                         /var/www/service/usercode/973543283/a.out
  024c1000-024e2000 rw-p 00000000 00:00 0                                  [heap]
  7ff45260e000-7ff452624000 r-xp 00000000 b6:4f3a1 105                     /lib/x86_64-linux-gnu/libgcc_s.so.1
  7ff452624000-7ff452823000 ---p 00016000 b6:4f3a1 105                     /lib/x86_64-linux-gnu/libgcc_s.so.1
  7ff452823000-7ff452824000 rw-p 00015000 b6:4f3a1 105                     /lib/x86_64-linux-gnu/libgcc_s.so.1
  7ff452824000-7ff4529e3000 r-xp 00000000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
  7ff4529e3000-7ff452be3000 ---p 001bf000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
  7ff452be3000-7ff452be7000 r--p 001bf000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
  7ff452be7000-7ff452be9000 rw-p 001c3000 b6:4f3a1 10849                   /lib/x86_64-linux-gnu/libc-2.23.so
  7ff452be9000-7ff452bed000 rw-p 00000000 00:00 0 
  7ff452bed000-7ff452c13000 r-xp 00000000 b6:4f3a1 10838                   /lib/x86_64-linux-gnu/ld-2.23.so
  7ff452de1000-7ff452de4000 rw-p 00000000 00:00 0 
  7ff452e0f000-7ff452e12000 rw-p 00000000 00:00 0 
  7ff452e12000-7ff452e13000 r--p 00025000 b6:4f3a1 10838                   /lib/x86_64-linux-gnu/ld-2.23.so
  7ff452e13000-7ff452e14000 rw-p 00026000 b6:4f3a1 10838                   /lib/x86_64-linux-gnu/ld-2.23.so
  7ff452e14000-7ff452e15000 rw-p 00000000 00:00 0 
  7ffee1fbb000-7ffee1fd0000 rw-p 00000000 00:00 0                          [stack]
  7ffee1ff9000-7ffee1ffb000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  
  Abort signal from abort(3) (SIGABRT)
  
  
  
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct merchandise
  {
    char name[50];
    char manufacturer[50];
    double quantity;
    double price;
    char unit[2];
    struct merchandise *next;
  };
  
  struct shop
  {
    struct merchandise *first;
  };
  
  struct shop *create_head ()
  {
    struct shop *head = (struct shop *)malloc(sizeof(struct shop));
    head -&gt; first = NULL;
    return head;
  }
  
  void insert (struct shop *head)
  {
    struct merchandise *temp = (struct merchandise *) malloc (sizeof(struct merchandise));
    temp -&gt; next = NULL;
  
  
    char name[50];
    char manufacturer[50];
    double quantity;
    double price;
    char unit[2];
  
    scanf("%s %s %lf %lf %s\n", name, manufacturer, &amp;quantity, &amp;price, unit);
  
    if (head -&gt; first == NULL)
      head -&gt; first = temp;
  
    else
    {
      struct merchandise *temp2 = head -&gt; first;
      while (temp2 -&gt; next != NULL)
        temp2 = temp2 -&gt; next;
  
      temp2 -&gt; next = temp;
      temp -&gt; next = NULL;
    }
  
    strcpy(temp -&gt; name, name);
    strcpy(temp -&gt; manufacturer, manufacturer);
    temp -&gt; quantity = quantity;
    temp -&gt; price = price;
    strcpy(temp -&gt; unit, unit);
  }
  
  void display (struct shop *head)
  {
    struct merchandise *temp = head -&gt; first;
    while (temp != NULL)
    {
      printf("Name=%s, Manufacturer=%s, Quantity=%lf, Price=%lf, MJ=%s\n", temp -&gt; name, temp -&gt; manufacturer, temp -&gt; quantity, temp -&gt; price, temp -&gt; unit);
      temp = temp -&gt; next;
    }
    printf("\n");
  }
  
  int main()
  {
    char select[20];
    struct shop *head = create_head();
    while (scanf("%s", select) &gt; 0)
    {
      if (strcmp(select, "insert") == 0)
      {
        if (head-&gt;first != NULL)
        {
          free(head);
          struct shop *head = create_head();
        }
        insert (head);
      }
  
      if (strcmp(select, "display") == 0)
        display (head);
    }
  
  
    return 0;
  }
  </code></pre>