title: Need help in strtok function when the first token is null
body: <p>Need help in strtok function</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  int main()
  {
      char string[100], *ptr = NULL;
  
      memset(string, 0, 100);
      strcpy(string, "abc#efg#xyz");
  
      ptr = strtok(string, "#");
      fprintf(stderr, "ptr = [%s]\n", ptr);
  
      ptr = strtok(NULL, "#");
      fprintf(stderr, "ptr = [%s]\n", ptr);
  
      ptr = strtok(NULL, "#");
      fprintf(stderr, "ptr = [%s]\n", ptr);
      return 0;
  }
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>ptr = [abc]
  ptr = [efg]
  ptr = [xyz]
  </code></pre>
  
  <p>This is fine, but when if the first token is null then the first call to strtok returns the second token.  My understanding is that it will return a null in the first call as the token is not present.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  int main()
  {
      char string[100], *ptr = NULL;
  
      memset(string, 0, 100);
      strcpy(string, "#efg#xyz");
  
      ptr = strtok(string, "#");
      fprintf(stderr, "ptr = [%s]\n", ptr);
  
      ptr = strtok(NULL, "#");
      fprintf(stderr, "ptr = [%s]\n", ptr);
  
      ptr = strtok(NULL, "#");
      fprintf(stderr, "ptr = [%s]\n", ptr);
      return 0;
  }***
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>ptr = [efg]
  ptr = [xyz]
  ptr = [(null)]
  </code></pre>