title: C Token Parsing
body: <p>So I'm trying to implement a token parser that doesn't use any C library functions like strtok() etc but I'm having a few issues with access violations and after reading several similar questions on here still haven't got it nailed down. Anyone willing to offer some pointers?</p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      int maxTokens = 10;
      char* tokens[10];
  
      int i;
      for(i = 0; i &lt; maxTokens; i++)
      {
          tokens[i] = NULL;
      }
  
      char* str = "This,is,a,test,string";
  
      int result = parseLine(str, ',', tokens, maxTokens);
  
      printf("%d tokens were found!", result);
      system("PAUSE");
      return 0;
  }
  
  int parseLine(char* str, char delimeter, char* tokens[], int maxTokens)
  {
      char* srcStr = str;
  
      int strlen = 0;
      int tokenCount = 0;
  
      if(srcStr[strlen] != delimeter &amp;&amp; srcStr[strlen] != '\0')
      {
          tokens[tokenCount] = (char*) malloc(sizeof(char)*strlen+1);
          tokens[tokenCount] = &amp;srcStr[strlen];
          tokenCount++;
      }
  
      while(srcStr[strlen] != '\0')
      {
          if(srcStr[strlen] == delimeter)
          {
              tokens[tokenCount-1][strlen] = '\0';
              if(srcStr[strlen+1] != '\0')
              {
                  tokens[tokenCount] = (char*) malloc(sizeof(char)*strlen+1);
                  tokens[tokenCount] = &amp;srcStr[++strlen];
                  tokenCount++;
              }
          }
          else
          {
              strlen++;
          }
      }
  
      return tokenCount;
  }
  </code></pre>