title: Remove spaces from char-array by using a function with a pointer argument
body: <p>So I'm studying this book, and I came across an exercise which (briefly) wants me to remove all white spaces of a char-array by using a function: void removeSpaces(char* s)</p>
  
  <p>[iostream, cstring are included and SIZE is defined]</p>
  
  <p>This is main():</p>
  
  <pre><code>int main() {
    char a[SIZE] = "a bb ccc d";
    cout &lt;&lt; a &lt;&lt; endl; // a bb ccc d
    removeSpaces(a);
    cout &lt;&lt; a &lt;&lt; endl; // a bb ccc d instead of abbcccd
  }
  </code></pre>
  
  <p>This is removeSpaces():</p>
  
  <pre><code>void removeSpace(char* s) {
    int size = strlen(s);
  
    char* cpy = s;  // an alias to iterate through s without moving s
    char* temp = new char[size]; // this one produces the desired string
    s = temp; // s points to the beginning of the desired string
  
    while(*cpy) {
        if(*cpy == ' ')
            cpy++;
        else
            *temp++ = *cpy++;
    }
  
    cout &lt;&lt; s &lt;&lt; endl; // This prints out the desired result: abbcccd
  }
  </code></pre>
  
  <p>(My choice of names isn't ideal, but nevermind that now.) So my function basically does what I want it to do, except that the result has no effect outside of the function's scope. How can I accomplish that? What am I missing, what am I doing wrong?</p>