title: Space removal from a String - In Place C style with Pointers
body: <p>So, its a pretty simple problem and I know the solution which is a simple function like the one below:</p>
  
  <pre><code>  void removeSpaces(char* s) {
      char* source = s;
      char* dest = s;
  
      while(*source) {
          if(*source == ' ') {
              source++;
          } else {
              *dest++ = *source++;
          }
      }
  
      *dest = 0;
  }
  </code></pre>
  
  <p>I am working in Visual C++ 2008 Express edition</p>
  
  <p>When I call it with the following it works fine without any issues i.e it removes all the spaces:</p>
  
  <pre><code>int main() {
      char input[50] = "I       like            2% milk";
      removeSpaces(input);
      cout&lt;&lt;input;
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>But, the problem is when I call it by changing the string declaration to this:</p>
  
  <pre><code>char * input = "I       like            2% milk";
  </code></pre>
  
  <p>I get a exception (some kind of access violation)</p>
  
  <p>The exception is showing on this line of code of the removeSpace function</p>
  
  <pre><code>*dest++ = *source++;
  </code></pre>
  
  <p>Can anyone elaborate as to why is this happening?</p>