-
August 18th, 2014, 04:46 PM
#1
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.
-
August 18th, 2014, 06:48 PM
#2
Re: How to understand this simple piece of code?
The debugger reveals all.
-
August 18th, 2014, 07:18 PM
#3
Re: How to understand this simple piece of code?
Originally Posted by GCDEF
The debugger reveals all.
+1 Yeah Larry, why ask a question when a couple of minutes in a debugger will tell you everything?
-
August 19th, 2014, 03:07 AM
#4
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 19th, 2014, 06:35 AM
#5
Re: How to understand this simple piece of code?
Originally Posted by LarryChen
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.
-
August 19th, 2014, 06:53 AM
#6
Re: How to understand this simple piece of code?
Originally Posted by 2kaud
To which 'r' are you referring - r in reverse() or r in main()?
there's an r in main() ?
-
August 19th, 2014, 08:30 AM
#7
Re: How to understand this simple piece of code?
Last edited by 2kaud; August 19th, 2014 at 08: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++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 19th, 2014, 09:00 AM
#8
Re: How to understand this simple piece of code?
Originally Posted by monarch_dodra
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.
-
August 19th, 2014, 09:03 AM
#9
Re: How to understand this simple piece of code?
Originally Posted by 2kaud
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.
-
August 19th, 2014, 09:19 AM
#10
Re: How to understand this simple piece of code?
Originally Posted by LarryChen
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
-
August 19th, 2014, 09:57 AM
#11
Re: How to understand this simple piece of code?
Originally Posted by LarryChen
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.
-
August 19th, 2014, 10:17 AM
#12
Re: How to understand this simple piece of code?
Originally Posted by LarryChen
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:
Originally Posted by monarch_dodra
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.
-
August 19th, 2014, 10:31 AM
#13
Re: How to understand this simple piece of code?
Originally Posted by GCDEF
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.
-
August 19th, 2014, 10:40 PM
#14
Re: How to understand this simple piece of code?
Originally Posted by LarryChen
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|