After reading the code on the top answer here,
I have a couple of questions WHY this answer successfully works as it should be.
I have walked through myself to this code, but still do not know WHY str
achieves to get the expected string.
I DO NOT have enough reputation to leave comments on this answer, so I decided to open a new question.
The following is the code provided by @dasblinkenlight. (I changed the input string
for testing purposes.)
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;
}
So, this is the walk through of the code, in my understanding.
In
line 1
, both*pr
and*pw
are pointing to the first element of the inputstring
*pr ==> 'a'
*pw ==> 'a'
Inside the while
loop.
The result will be separated by |
per iteration.
(1st iteration) (2nd iteration) (3rd iteration) (4th iteration)
*pr
(line 3) ========> 'a'
| 'b'
| 'c'
| 'd'
*pw = *pr++
(line 4) ==> 'a' = 'b'
| 'b' = 'c'
| 'c' = 'd'
| 'c' = '\0'
(*pw != c)
(line 5) ==> 'b' != 'c'
(true)| 'c' != 'c'
(false)| 'd' != 'c'
(true) | '\0' != 'c'
(true)
pw
(after, pw += (*pw != c)
) ==> str[1]
, 'b'
| str[1]
, 'c'
| str[2]
, 'c'
| str[3]
, 'd'
So now, if my walkthrough is correct, I should have str
, with the value of bd
.
However, running this on the code editor, it will give me back the expected answer which is abd
.
I double-checked my walk through with editor, so I am pretty sure in the changes of values in each variable.
If you can help with understanding why str
ends up with the value of abd
, please let me know.