CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2007
    Posts
    40

    Question what do these operators do?

    *dst++=total;
    *kp++;

    do they move the pointer one step forward or something?

    thanks

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: what do these operators do?

    Quote 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++;

    Quote 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().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: what do these operators do?

    Quote Originally Posted by laserlight View Post
    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.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: what do these operators do?

    Quote Originally Posted by laserlight View Post
    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...

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: what do these operators do?

    Quote 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.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Nov 2003
    Posts
    1,405

    Re: what do these operators do?

    Quote Originally Posted by EVS View Post
    *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
  •  





Click Here to Expand Forum to Full Width

Featured