title: copying string from strtok
body: <p>I need to divide a C string into tokens. I thought that strtok will be my best try, but I'm getting very strange results... </p>
  
  <p>Here is my test program. In this example I will get 3 tokens with "##" separator but when I try to work with the ones I supposedly had copied, only the third one is shown correctly.. the other two look corrupted or something... I don't know... ?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;malloc.h&gt;
  
  #define TAM 3 //elements
  
  char** aTokens(char* str, char* delimitador)
  {
   char* pch;
   char** tokens;
   int i = 0; 
   tokens = (char**)malloc(sizeof(char*)*TAM);
  
   pch = strtok(str, delimitador);
  
   while(pch != NULL)
   {
    tokens[i] = (char*)malloc((sizeof(strlen(pch))+1) * sizeof(char));
    strcpy(tokens[i], pch);
    pch = strtok(NULL, delimitador);
    i++;
   }
  
   return tokens;
  }
  
  int main ()
  {
   char str[] = "30117700,1,TITULAR,SIGQAA070,1977/11/30,M,1,14000,0.00,6600.00,10.00,2011/09/01,2012/09/01,0|17,0.00,NO,0,0,0.00, ,##30117700,1,TITULAR,SIGQAA070,1977/11/30,M,1,14000,0.00,6600.00,10.00,2011/09/01,2012/09/01,0|17,0.00,NO,0,0,0.00, ,##30117700,1,TITULAR,SIGQAA070,1977/11/30,M,1,14000,0.00,6600.00,10.00,2011/09/01,2012/09/01,0|17,0.00,NO,0,0,0.00, ,";
  
   char** tokens;
   int i;
  
   tokens = aTokens(str, "##");
  
   for(i = 0; i&lt;TAM; i++)
    printf("%d -- %s\n", strlen(tokens[i]), tokens[i]);
  
   //Clean
   //for(i = 0; i&lt;TAM; i++)
    //free(tokens[i]);
  
   //free(tokens);
  
   return 0;
  }
  </code></pre>
  
  <p>output with GCC on Linux:</p>
  
  <pre><code>13 -- 30117700,1,T   &lt;---- ?
  13 -- 30117700,1,T   &lt;----- ?
  115 -- 30117700,1,TITULAR,SIGQAA070,1977/11/30,M,1,14000,0.00,6600.00,10.00,2011/09/01,2012/09/01,0|17,0.00,NO,0,0,0.00, ,
  </code></pre>
  
  <p>I have commented the "clean" section because it provides lots of runtime error too ... :(  </p>
  
  <p>Help please!!</p>