title: [Code Walkthrough]How to remove all occurrences of a given character from string in C?
body: <p>After reading the code on <a href="https://stackoverflow.com/questions/9895216/how-to-remove-all-occurrences-of-a-given-character-from-string-in-c">the top answer here</a>, <br>
  I have a couple of questions WHY this answer successfully works as it should be.<br>
  I have walked through myself to this code, but still do not know WHY <code>str</code> achieves to get the expected string.<br>
  I DO NOT have enough reputation to leave comments on this answer, so I decided to open a new question.</p>
  
  <p>The following is the code provided by <a href="https://stackoverflow.com/users/335858/dasblinkenlight">@dasblinkenlight</a>. (I changed the input <code>string</code> for testing purposes.)</p>
  
  <pre><code>void remove_all_chars(char* str, char c) {
      char *pr = str, *pw = str;           // line 1
                                           // Inside the while loop
      while (*pr) {                        // line 3
          *pw = *pr++;                     // line 4
          pw += (*pw != c);                // line 5
          printf("str: %s\n", str);       // FYI, this will always print `abcd`, isn't it weird, if `str` is mutated in the end of this program?
      }
      *pw = '\0';
  }
  
  int main() {
      char str[] = "abcd";
      remove_all_chars(str, 'c');
      printf("'%s'\n", str);
      return 0;
  }
  </code></pre>
  
  <p>So, this is the walk through of the code, in my understanding. <br></p>
  
  <blockquote>
    <p>In <code>line 1</code>, both <code>*pr</code> and <code>*pw</code> are pointing to the first element of
    the input <code>string</code> <br> <code>*pr ==&gt; 'a'</code> <br> <code>*pw ==&gt; 'a'</code></p>
  </blockquote>
  
  <p>Inside the <code>while</code> loop. <br>The result will be separated by <code>|</code> per iteration.<br> 
  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(1st iteration) &nbsp; (2nd iteration) &nbsp; (3rd iteration) &nbsp; (4th iteration)<br>
  <code>*pr</code> (line 3) ========>&nbsp; <code>'a'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; <code>'b'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; <code>'c'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; <code>'d'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
  <code>*pw = *pr++</code> (line 4) ==> <code>'a' = 'b'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| <code>'b' = 'c'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| <code>'c' = 'd'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| <code>'c' = '\0'</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
  <code>(*pw != c)</code> (line 5) ==> <code>'b' != 'c'</code> (true)| <code>'c' != 'c'</code> (false)| <code>'d' != 'c'</code> (true) | <code>'\0' != 'c'</code> (true)<br>
  <code>pw</code>(after, <code>pw += (*pw != c)</code>) ==> <code>str[1]</code>,  <code>'b'</code> | <code>str[1]</code>, <code>'c'</code> | <code>str[2]</code>, <code>'c'</code> | <code>str[3]</code>, <code>'d'</code> </p>
  
  <p>So now, if my walkthrough is correct, I should have <code>str</code>, with the value of <code>bd</code>. <br>
  However, running this on the code editor, it will give me back the expected answer which is <code>abd</code>.</p>
  
  <p>I double-checked my walk through with editor, so I am pretty sure in the changes of values in each variable.<br>
  If you can help with understanding why <code>str</code> ends up with the value of <code>abd</code>, please let me know.</p>