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.
Re: How to understand this simple piece of code?
The debugger reveals all.
Re: How to understand this simple piece of code?
Quote:
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?
Re: How to understand this simple piece of code?
Quote:
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()?
Re: How to understand this simple piece of code?
Quote:
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.
Re: How to understand this simple piece of code?
Quote:
Originally Posted by
2kaud
To which 'r' are you referring - r in reverse() or r in main()?
there's an r in main() ? :p
Re: How to understand this simple piece of code?
Quote:
Originally Posted by
oreubens
there's an r in main() ? :p
:) :rolleyes:
Re: How to understand this simple piece of code?
Quote:
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.
Re: How to understand this simple piece of code?
Quote:
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.
Re: How to understand this simple piece of code?
Quote:
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!
Re: How to understand this simple piece of code?
Quote:
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.
Re: How to understand this simple piece of code?
Quote:
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:
Quote:
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?
Re: How to understand this simple piece of code?
Quote:
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.
Re: How to understand this simple piece of code?
Quote:
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?