c - Suggestions for concise index handling in circular buffer -


i've implemented circular buffer, , concise means of updating buffer pointer while handling wrap-around.

assuming array of size 10, first response like:

size_t ptr = 0;   // work... p = ++p % 10; 

static analysis, gcc -wall -wextra, rightly slapped wrist unspecified behavior due sequence point violation. obvious fix like:

p++; p %= 10; 

however, looking more concise, (i.e., one-liner) "encapsulate" operation. suggestions? other p++; p%= 10; :-)

p = (p + 1) % n; 

or avoid modulo:

p = ((n-1) == p) ? 0 : (p+1); 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -