I have implemented a circular buffer, and I would like to get a short way to update the buffer pointer with proper wrapper handling.
Assuming an array of size 10, my first answer was something like this:
size_t ptr = 0; // do some work... p = ++p % 10;
Static analysis, as well as gcc -Wall -Wextra , correctly hit the wrist for unspecified behavior due to a violation of the sequence point. The obvious fix is โโsomething like:
p++; p %= 10;
However, I was looking for something more concise (i.e., single-line) to "encapsulate" this operation. Suggestions? Except p ++; p% = 10; :-)
c sequence-points c99
Throwback1986
source share