title: strtok not working as expected, works only for first few iterations
body: <p>I'm using strtok to split up a string, it works as expected for the first 4 iterations, but starts messing up after that. The program is supposed to take a line such as "david 1 2 3 4 5" and print out the name plus the sum of the numbers "david 15"... However I am having a problem with strtok.</p>
  
  <p>EDIT: I make number a pointer for later uses of the code (this is only a snippet)</p>
  
  <p>This is my C script:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  char str1[50];
  char *token;
  char *end;
  long int *number;
  int loop = 1;
  while(loop){
      fgets(str1,50,stdin);
      token = strtok(str1," ");
      printf("%s ",token);
      *number = 0;
      while(token != NULL){
          token = strtok(NULL," ");
          if(token != NULL){
              *number = *number + strtol(token,&amp;end,0);
          }
      }
      printf("%li\n",*number);
  }
  </code></pre>
  
  <p>With this input:</p>
  
  <pre><code>David 10 10 10 10 10
  </code></pre>
  
  <p>I get this output:</p>
  
  <pre><code>David 40
  </code></pre>
  
  <p>The expected output is:</p>
  
  <pre><code>David 50
  </code></pre>
  
  <p>Suspecting strtok, I put these test print statements:</p>
  
  <pre><code>char str1[50];
  char *token;
  char *end;
  long int *number;
  int loop = 1;
  while(loop){
      fgets(str1,50,stdin);
      token = strtok(str1," ");
      printf("%s\n",token);
      *number = 0;
      while(token != NULL){
          printf("before new token: %s\n",token);    // &lt;---HERE
          token = strtok(NULL," ");
          printf("after new token: %s\n\n",token);   // &lt;---HERE
          if(token != NULL){
              *number = *number + strtol(token,&amp;end,0);
          }
      }
      printf("%li\n",*number);
  }  
  </code></pre>
  
  <p>And with the same input of:
  </p>
  
  <pre class="lang-none prettyprint-override"><code>David 10 20 30 40 50
  </code></pre>
  
  <p>I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>David
  before new token: David
  after new token: 10
  
  before new token: 10
  after new token: 20
  
  before new token: 20
  after new token: 30
  
  before new token: 30
  after new token: 4&lt;
  
  before new token: 4@
  after new token: (null)
  
  64
  </code></pre>
  
  <p>As you an see it works fine during the first few iterations, but after that things start going bad. If anyone has any idea what is happening, I would greatly appreciate the advice :) thanks!</p>