Post-increment on a dereferenced pointer?

This is one of those little gotcha’s that make C and C++ so much fun. If you want to bend your brain, figure out this one:

while (*dst++ = *src++) ;

It’s a string copy. The pointers keep getting incremented until a character with a value of zero is copied. Once you know why this trick works, you’ll never forget how ++ works on pointers again.

P.S. You can always override the operator order with parentheses. The following will increment the value pointed at, rather than the pointer itself:

(*our_var_ptr)++;

Leave a Comment