title: Access Violation With Pointers? - C++
body: <p>I've written a simple string tokenizing program using pointers for a recent school project. However, I'm having trouble with my <code>StringTokenizer::Next()</code> method, which, when called, is supposed to return a pointer to the first letter of the next word in the char array. I get no compile-time errors, but I get a runtime error which states:</p>
  
  <pre><code>Unhandled exception at 0x012c240f in Project 5.exe: 0xC0000005: Access violation reading location 0x002b0000.
  </code></pre>
  
  <p>The program currently tokenizes the char array, but then stops and this error pops up. I have a feeling it has to do with the <code>NULL</code> checking I'm doing in my <code>Next()</code> method.</p>
  
  <p>So how can I fix this?</p>
  
  <p>Also, if you notice anything I could do more efficiently or with better practice, please let me know.</p>
  
  <p><strong><em>Thanks!!</em></strong></p>
  
  <hr>
  
  <p><strong>StringTokenizer.h:</strong></p>
  
  <pre><code>#pragma once
  
  class StringTokenizer
  {
  public:
  StringTokenizer(void);
  StringTokenizer(char* const, char);
  char* Next(void);
  ~StringTokenizer(void);
  private:
  char* pStart;
  char* pNextWord;
  char delim;
  };
  </code></pre>
  
  <hr>
  
  <p><strong>StringTokenizer.cpp:</strong></p>
  
  <pre><code>#include "stringtokenizer.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  StringTokenizer::StringTokenizer(void)
  {
  pStart = NULL;
  pNextWord = NULL;
  delim = 'n';
  }
  
  StringTokenizer::StringTokenizer(char* const pArray, char d)
  {
  pStart = pArray;
  delim = d;
  }
  
  char* StringTokenizer::Next(void)
  {
  pNextWord = pStart;
  if (pStart == NULL) { return NULL; }
  
  while (*pStart != delim) // access violation error here
  {
      pStart++;
  }
  
  if (pStart == NULL) { return NULL; }
  
  *pStart = '\0'; // sometimes the access violation error occurs here
  pStart++;
  
  return pNextWord;
  }
  
  StringTokenizer::~StringTokenizer(void)
  {
  delete pStart;
  delete pNextWord;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Main.cpp:</strong></p>
  
  <pre><code>// The PrintHeader function prints out my
  // student info in header form
  // Parameters - none
  // Pre-conditions - none
  // Post-conditions - none
  // Returns - void
  void PrintHeader();
  
  int main ( )
  {
  const int CHAR_ARRAY_CAPACITY = 128;
  const int CHAR_ARRAY_CAPCITY_MINUS_ONE = 127;
  
  // create a place to hold the user's input
  // and a char pointer to use with the next( ) function
  char words[CHAR_ARRAY_CAPACITY];
  char* nextWord;
  
  PrintHeader();
  
  cout &lt;&lt; "\nString Tokenizer Project";
  cout &lt;&lt; "\nyour name\n\n";
  cout &lt;&lt; "Enter in a short string of words:";
  cin.getline ( words, CHAR_ARRAY_CAPCITY_MINUS_ONE );
  
  // create a tokenizer object, pass in the char array
  // and a space character for the delimiter
  StringTokenizer tk( words, ' ' );
  
  // this loop will display the tokens
  while ( ( nextWord = tk.Next ( ) ) != NULL )
  {
      cout &lt;&lt; nextWord &lt;&lt; endl;
  }
  
  
  system("PAUSE");
  return 0;
  }
  </code></pre>
  
  <hr>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Okay, I've got the program working fine now, as long as the delimiter is a space. But if I pass it a `/' as a delim, it comes up with the access violation error again. Any ideas?</p>
  
  <p>Function that works with spaces:</p>
  
  <pre><code>char* StringTokenizer::Next(void)
  {
  pNextWord = pStart;
  
  if (*pStart == '\0') { return NULL; }
  
  while (*pStart != delim)
  {
      pStart++;
  }
  
  if (*pStart = '\0') { return NULL; }
  
  *pStart = '\0';
  pStart++;
  
  return pNextWord;
  }
  </code></pre>