either
*p = getchar(); p++;
ORp++; p* = getchar()
[sic]
Well actuall no. It's either:
*p = getchar(); p++
or
old_p = p++; *old_p = getchar()
In your interpretation, you could either write at *p
or *(p + 1)
which is not true. In both interpretations, you write to where p
originally pointed to, regardless of when the compiler decides to put the instruction that changes p
. If it decides to put it before the write to *p
, then it would have to make sure it keeps the old value to later write to (*old_p
).
The suffix --
and ++
operators are actually quite nice shortcuts. Take this example:
size_t strlen(const char *str)
{
size_t len = 0;
while (str[len++]);
return len - 1;
}
This is a very concise implementation of strlen
using suffix ++
.
From C11, 6.5.2.4.2 (emphasis mine):
The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The value computation of the result is sequenced before the side effect of updating the stored value of the operand. With respect to an indeterminately-sequenced function call, the operation of postfix ++ is a single evaluation. Postfix ++ on an object with atomic type is a read-modify-write operation with memory_order_seq_cst memory order semantics.