How to understand this simple piece of code?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: How to understand this simple piece of code?

  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    How to understand this simple piece of code?

    Here is the code,
    Code:
    void reverseStr(char* l, char* r)
    {
    	char* p = l;
    	char t;
    	int len = 0;
    
    	while(*p)
    	{
    		*r++ = *p++; 
    		len++;
    	}
    
    	*r = '\0';
    	
    	p = r-len;
    	r = r-1;
    
    	while(p<r)
    	{
    		t = *r;
    		*r-- = *p;
    		*p++ = t;		 
    	}
    
    }
    
    int main(int argc, char* argv[])
    {
    	char s[24] = "abcdefg";
    	char r[24];
    
        reverseStr(s, r);
    
    	cout<<r<<endl;
    	return 0;
    }
    The output is "gfedcba". But I would expect it to be "dcba". Take a look at
    while(p<r)
    {
    t = *r;
    *r-- = *p;
    *p++ = t;
    }
    r eventually will move to the middle of "gfedcba", which is "dcba". How come r points to 'g' finally? Thanks.

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

    Re: How to understand this simple piece of code?

    The debugger reveals all.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,586

    Re: How to understand this simple piece of code?

    Quote Originally Posted by GCDEF View Post
    The debugger reveals all.
    +1 Yeah Larry, why ask a question when a couple of minutes in a debugger will tell you everything?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    Re: How to understand this simple piece of code?

    r eventually will move to the middle of "gfedcba", which is "dcba". How come r points to 'g' finally? Thanks.
    To which 'r' are you referring - r in reverse() or r in main()?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to understand this simple piece of code?

    Quote Originally Posted by LarryChen View Post
    r eventually will move to the middle of "gfedcba", which is "dcba". How come r points to 'g' finally? Thanks.
    r is passed by value, so any changes to r itself will not be seen at the end of the call: Even if in "reverseStr", r points to the middle of the string, the one in main will be unchanged.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to understand this simple piece of code?

    Quote Originally Posted by 2kaud View Post
    To which 'r' are you referring - r in reverse() or r in main()?
    there's an r in main() ?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    Re: How to understand this simple piece of code?

    Quote Originally Posted by oreubens View Post
    there's an r in main() ?
    Last edited by 2kaud; August 19th, 2014 at 09:34 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to understand this simple piece of code?

    Quote Originally Posted by monarch_dodra View Post
    r is passed by value, so any changes to r itself will not be seen at the end of the call: Even if in "reverseStr", r points to the middle of the string, the one in main will be unchanged.
    Thanks for your reply. I agree that r in reverseStr points to the middle of the string. But how come the r in main points to the beginning of the string? That is really my question.

  9. #9
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to understand this simple piece of code?

    Quote Originally Posted by 2kaud View Post
    To which 'r' are you referring - r in reverse() or r in main()?
    The r in reverse points to the middle of the string but the r in main points to the beginning of the string. I don't understand why? Thanks.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,008

    Re: How to understand this simple piece of code?

    Quote Originally Posted by LarryChen View Post
    The r in reverse points to the middle of the string but the r in main points to the beginning of the string. I don't understand why?
    Set a breakpoint at the beginning of the reverseStr function.
    Then press F5.
    Then debug your code using F10 and you will understand it!
    Victor Nijegorodov

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

    Re: How to understand this simple piece of code?

    Quote Originally Posted by LarryChen View Post
    The r in reverse points to the middle of the string but the r in main points to the beginning of the string. I don't understand why? Thanks.
    Have you tried debugging? r in reverse points to the beginning of an unitialized 24 byte char array. I and I assume others don't know what you mean by points to the middle of the string or why you think that.

  12. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to understand this simple piece of code?

    Quote Originally Posted by LarryChen View Post
    The r in reverse points to the middle of the string but the r in main points to the beginning of the string. I don't understand why? Thanks.
    Are you kidding? I mean, seriously? The answer is in the reply you *just* quoted:

    Quote Originally Posted by monarch_dodra View Post
    r is passed by value, so any changes to r itself will not be seen at the end of the call: Even if in "reverseStr", r points to the middle of the string, the one in main will be unchanged.
    How can you not understand this?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  13. #13
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to understand this simple piece of code?

    Quote Originally Posted by GCDEF View Post
    Have you tried debugging? r in reverse points to the beginning of an unitialized 24 byte char array. I and I assume others don't know what you mean by points to the middle of the string or why you think that.
    Yes, I did debug. Now I think I understand why. Within the function reverse, r does point to the middle of the string. But since r is passed by value, it won't change the address of r in main. r in main still points to the beginning of an uninitialized array. Silly me. Thanks a lot.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,586

    Re: How to understand this simple piece of code?

    Quote Originally Posted by LarryChen View Post
    Yes, I did debug. Now I think I understand why. Within the function reverse, r does point to the middle of the string. But since r is passed by value, it won't change the address of r in main. r in main still points to the beginning of an uninitialized array. Silly me. Thanks a lot.
    Any particular reason you didn't start answering the question by stepping through with the debugger first?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)