title: String parsing in C using strtok
body: <p>I've got this little source code, made for testing the parsing of a string similar to variable <code>string</code> I need to use in other project</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  
  int main (void)
  {
      char string[] = "C-AC-2C-3C-BOB";
      char* s;
      char* hand[3];
      char* usr;
  
  
      s = (char*) calloc(1, sizeof(char));
      hand[1] = (char*) calloc(3, sizeof(char));
      hand[2] = (char*) calloc(3, sizeof(char));
      hand[3] = (char*) calloc(3, sizeof(char));
      usr = (char*) calloc(21, sizeof(char));
  
      s = strtok (string,"-");
      hand[1] = strtok (NULL, "-");
      hand[2] = strtok (NULL, "-");
      hand[3] = strtok (NULL, "-");
      usr = strtok (NULL, "\0");
  
      printf("%s:%s:%s:%s:%s\n", s, hand[1], hand[2], hand[3], usr);
  
      return 0;
  }
  </code></pre>
  
  <p>The problem is that i got these <code>3C:AC:2C:3C:BOB</code> as result of printf instead of <code>C:AC:2C:3C:BOB</code>.</p>
  
  <p>-------EDIT-----</p>
  
  <p>Code without memory leaks. Problem remains</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  
  int main (void)
  {
      char string[] = "C-AC-2C-3C-BOB";
      char* s;
      char* hand[3];
      char* usr;
  
      s = strtok (string,"-");
      hand[1] = strtok (NULL, "-");
      hand[2] = strtok (NULL, "-");
      hand[3] = strtok (NULL, "-");
      usr = strtok (NULL, "\0");
  
      printf("%s:%s:%s:%s:%s\n", s, hand[1], hand[2], hand[3], usr);
  
      return 0;
  }
  </code></pre>