-
December 3rd, 2008, 03:01 PM
#1
what do these operators do?
*dst++=total;
*kp++;
do they move the pointer one step forward or something?
thanks
-
December 3rd, 2008, 03:28 PM
#2
Re: what do these operators do?
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.
-
December 3rd, 2008, 03:30 PM
#3
Re: what do these operators do?
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.
-
December 3rd, 2008, 03:51 PM
#4
Re: what do these operators do?
Originally Posted by GCDEF
*kp++ adds 1 to whatever kp is pointing to.
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++;
Originally Posted by Lindley
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.
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().
-
December 3rd, 2008, 04:13 PM
#5
Re: what do these operators do?
Originally Posted by laserlight
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++;
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().
I was having second thoughts as I was writing it, but figured somebody would straighten me out if I was wrong.
-
December 3rd, 2008, 04:21 PM
#6
Re: what do these operators do?
Originally Posted by laserlight
Actually, no, assuming that we are dealing with pointers, *kp++ increments the pointer kp, and returns what kp points to.
Actually, no...
it returns what kp USED TO PONT TO. Here is from MSDN:
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.
Also, the value is incremented NOT by 1, but
operand's value is increased by one unit of the appropriate type
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
December 3rd, 2008, 04:25 PM
#7
Re: what do these operators do?
Originally Posted by VladimirF
it returns what kp USED TO PONT TO. Here is from MSDN:
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.
Originally Posted by VladimirF
Also, the value is incremented NOT by 1, but
That means the same thing. In fact, the C++ standard states that "the value of the object is modified by adding 1 to it".
Last edited by laserlight; December 3rd, 2008 at 05:09 PM.
-
December 3rd, 2008, 06:55 PM
#8
Re: what do these operators do?
Originally Posted by EVS
*dst++=total;
*kp++;
do they move the pointer one step forward or something?
thanks
It's the C legacy. This level of terseness seldom is motivated in C++.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|