Incrementing pointers in C++
Why开发者_如何学Python are the two following code segments not equivalent?
void print (char* s) {
if (*s == '\0')
return;
print(s+1);
cout << *s;
}
void print (char* s) {
if (*s == '\0')
return;
print(++s);
cout << *s;
}
The ++
operator increments the pointer value, but then returns the original value ... so print(s++)
will print the value of s
before the increment, since even though it adds a value of 1 to s
, making the value stored at s
equal to s+1
, it still returns the original value of s
as the result of the operation. On the otherhand print(s+1)
prints the value after the increment, but very importantly does not modify the original value of s
. So the result of the statement s+1
is just a new temporary pointer value ... the original value of s
is not modified.
Furthermore, since you've incremented and changed the value of s
with the ++
operator, when you call cout
, you're now printing the value to wherever the new pointer is pointing (this could cause a crash or segmentation fault if you're not careful and there's no user accessible memory at the new memory location s
is pointing to). With s+1
, the value of s
remains unmodified, so the result of cout
will be to wherever s
was originally pointing.
Edit:
As Michael points out, this is actually a recursive function, so the second example simply keeps calling print()
with the same argument, since as mentioned before, the returned value from s++
is the original value of s
. That means you'll end up with a stack overflow at some point and just crash unless the value that s
pointed to was already the NULL character.
Since it looks like the OP changed print(s++)
to print(++s)
, which is hugely different, here's an explanation for this new version.
In the first example, you have:
print(s+1);
cout << *s;
s+1 does not modify s. So if s is 4, and you print(s+1)
, afterwards s will still be 4.
print(++s);
cout << *s;
In this case, ++s modifies the local value of s. It increments it by 1. So if it was 4 before print(++s)
, it will be 5 afterwards.
In both cases, a value equivalent to s+1 would be passed to the print function, causing it to print the next character.
So the difference between the 2 functions is that the first one will recursively print character #0, then 1, 2, 3, ..., while the second function prints 1, 2, 3, 4, ... (it skips the first character and prints the "\0" afterwards).
Example:
For the s+1
version, print("hello")
will result in h
e
l
l
o
For the ++s
version, print("hello")
will result in e
l
l
o
\0
Both of the expressions
s++
ands+1
are to do with increasing the position of the pointer itself, not the value contained at the pointer locationsThe value of
s++
is justs
, and the value ofs+1
is, well, one position further on thans
!- The value of
s
after executings++
is one position further on than it was before. After usings+1
, the value ofs
is unchanged.
Therefore the order they print out the letters is reversed!
I will try to explain an example of pre and post increment from which you can solve the question posted yourself.
#include <iostream>
void foo(int num)
{
std::cout << num << "\n" ;
}
int main()
{
int number = 10 ;
foo( number++ ) ;
foo( ++number ) ;
foo( number + 1 ) ;
getchar() ;
return 0 ;
}
Output:
10
12
13
Why 10?
foo( number++ ) ;
Post-increment operation is done on number. Meaning, value of number is first passed to foo
and then the value of number is incremented upon foo
return. So, after function return, number is 11.
Why 12?
foo( ++number ) ;
Pre-increment operation is done on number. Meaning, before even call to foo
, the value of number
is incremented to 12. And then it is passed to foo
. So, even after the function return, number is still 12.
Why 13?
It's just straight forward. Value of number is not modified but passed a value adding 1 to the value of number. In this process, number is not modified. So, even after function return, number is still 12.
Hope this helps to solve the problem yourself ( though in your case it is paper-pencil exercise ) :)
精彩评论