*dst++=total;
*kp++;
do they move the pointer one step forward or something?
thanks
Printable View
*dst++=total;
*kp++;
do they move the pointer one step forward or something?
thanks
The * deferences the pointer. ++ increments a value by 1.
*kp++ adds 1 to whatever kp is pointing to.
The first one to me is a good example of why white space is important.
That's just really bad coding. Not because it's wrong, but because it's hard to read and requires strong understanding of the order of operations to interpret.
Actually, no, assuming that we are dealing with pointers, *kp++ increments the pointer kp, and returns what kp points to. Since the expression stands alone, its net effect is equivalent to kp++;Quote:
Originally Posted by GCDEF
Dunno, that someone as experienced as GCDEF can make such a mistake seems to back up your argument, but on the other hand this idiom seems rather well known, considering that many people would have seen the while (*dest++ = *src++); implementation of strcpy().Quote:
Originally Posted by Lindley
Actually, no... :)
it returns what kp USED TO PONT TO. Here is from MSDN:
Also, the value is incremented NOT by 1, butQuote:
It is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator.
Quote:
operand's value is increased by one unit of the appropriate type
After thinking about it further, I would agree that my statement is ambiguous, and so is "it returns what kp used to point to". I intended to explain it from the point of view before the side effect has taken place, with the incrementing and returning as happening in parallel, but it could also be interpreted as happening in sequence, despite the use of "and" rather than "then". In other words, the frame of reference needs to be made clear, as in the MSDN wording, and also in the wording of the C and C++ standards.Quote:
Originally Posted by VladimirF
That means the same thing. In fact, the C++ standard states that "the value of the object is modified by adding 1 to it".Quote:
Originally Posted by VladimirF