title: C - Unexpected Segmentation Fault on strtok(...)
body: <p>I am using strtok(...) of the  library and it appears to be working fine until the end condition, where it results in a segmentation fault and program crash.  The API claims that strtok(...) will output a NULL when there are no more tokens to be found, which meant, I thought, that you had to catch this NULL in order to terminate any loops that you were running using strtok(...).  What do I need to do to catch this NULL to prevent my program from crashing?  I imagined the NULL was allowed for use as a terminating condition.</p>
  
  <p>I have prepared a SSCCE for you to observe this behavior.  I need strtok(...) to work for a much larger piece of software I am writing, and I am getting the exact same segmentation behavior.  The output at the command line is shown below this code vignette (yes I know you use &lt;...> to enclose libraries, but I was having difficulty getting this post to display the code libraries).  I am using gcc version 4.5.3, on a Windows 8 OS, and below shows two different flavors of how I imagine one could try to catch the NULL in a loop.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  
  main(){
    char* from = "12.34.56.78";
    char * ch = ".";
    char * token = strtok(from, ch);
    printf("%s\n",token);
    while(token != NULL){
      token = strtok(NULL, ch);
      printf("%s\n", token);
    }
    printf("Broke out of loop!");
    while(strcmp(token, 0) != 0){
      printf("%s\n",token);
      token = strtok(NULL, ch);
    }
  }
  </code></pre>
  
  <pre>
  ############ OUTPUT: ############
  
  $ ./test
  12
  34
  56
  78
  Segmentation fault (core dumped)
  </pre>