title: String reversal with pointers C++
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string">Why do I get a segmentation fault when writing to a string?</a>  </p>
  </blockquote>
  
  
  
  <p>I want to write a simple C++ function that reverses a
  <code>string</code>/<code>char[]</code> by only pointer arithmetic. I understand the concept
  and have code already typed up.</p>
  
  <p>I have the following .cpp file:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using std::cout;
  using std::endl;
  
  void reverse(char* target) //Requirements specify to have this argument
  {
      cout &lt;&lt; "Before :" &lt;&lt; target &lt;&lt; endl; // Print out the word to be reversed
      if(strlen(target) &gt; 1) // Check incase no word or 1 letter word is placed
      {
          char* firstChar = &amp;target[0]; // First Char of char array
          char* lastChar = &amp;target[strlen(target) - 1]; //Last Char of char array
          char temp; // Temp char to swap
          while(firstChar &lt; lastChar) // File the first char position is below the last char position
          {
              temp = *firstChar; // Temp gets the firstChar
              *firstChar = *lastChar; // firstChar now gets lastChar
              *lastChar = temp; // lastChar now gets temp (firstChar)
              firstChar++; // Move position of firstChar up one
              lastChar--; // Move position of lastChar down one and repeat loop
          }
      }
      cout &lt;&lt; "After :" &lt;&lt; target &lt;&lt; endl; // Print out end result.
  }
  
  void main()
  {
      reverse("Test"); //Expect output to be 'tseT'
  }
  </code></pre>
  
  <p>I've stepped through in the debugger several times but each time it
  crashes around the <code>temp = *firstChar</code> line in the while loop. It
  freezes up here and causes the program to stop running and unable to
  finish. Is there something I am simply overlooking or is there
  something deeper as to why I can't do it this way.</p>
  
  <p><strong>EDIT:</strong> There is an else condition, but I removed it for the sake of
  brevity. It was after the <code>if</code> statement and it just prompted that the
  word was 1 char or no word was put.</p>