3 ms·
> It increments t in the loop after dereferencing it which only changes the value pointed to by t. This is wrong. Postfix increment has a higher precedence th
by Figs 8y ago
> It increments t in the loop after dereferencing it which only changes the value pointed to by t.
This is wrong. Postfix increment has a higher precedence than the dereference operator.
- badrabbit 8y agoAre you sure? I'll need to test this. Always thought postfix increment and decrement applied after everything else was evaluated,and that's why prefix versions exist.
- Figs 8y agoYes. Here is a table that lists operator precedence in C: https://en.cppreference.com/w/c/language/operator_precedence https://en.cppreference.com/w/c/language/operator_precedence Note that postfix increment is at the highest precedence and the dereference operator is at the second highest precedence. The postfix operators behave in a bit of an interesting way; the expression "t++" evaluates to just "t" but increments t as a side effect. Consequently, it appears to update t after the expression it is part of, even though it is one of the first things evaluated. If you actually test the function, you will find that it works provided that you make r static or global instead of a local stack variable -- and, of course, carefully mind the restriction on the length of the input strings to avoid overflowing the buffer.
- badrabbit 8y agoThank you! I learned something new.