C++ 中的指针递增
为什么下面的两个代码段不等价?
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;
}
Why 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
++
运算符递增指针值,但随后返回原始值...因此print(s++)
将打印s
的值在递增之前,因为即使它将值 1 添加到s
中,使存储在s
中的值等于s+1
,它仍然返回s
的原始值作为操作的结果。另一方面,print(s+1)
打印增量后的值,但非常重要的是不会修改s
的原始值。所以语句s+1
的结果只是一个新的临时指针值...s
的原始值没有被修改。此外,由于您已经使用
++
运算符增加并更改了s
的值,因此当您调用cout
时,您现在正在打印值指向新指针所指向的位置(如果您不小心并且在s
指向的新内存位置没有用户可访问的内存,这可能会导致崩溃或分段错误)。使用s+1
时,s
的值保持不变,因此cout
的结果将指向s
的任意位置原本是指着的。编辑:
正如迈克尔指出的,这实际上是一个递归函数,因此第二个示例只是使用相同的参数继续调用
print()
,因为如前所述,从s++< 返回的值/code> 是
s
的原始值。这意味着您最终会在某个时刻出现堆栈溢出并崩溃,除非s
指向的值已经是 NULL 字符。The
++
operator increments the pointer value, but then returns the original value ... soprint(s++)
will print the value ofs
before the increment, since even though it adds a value of 1 tos
, making the value stored ats
equal tos+1
, it still returns the original value ofs
as the result of the operation. On the otherhandprint(s+1)
prints the value after the increment, but very importantly does not modify the original value ofs
. So the result of the statements+1
is just a new temporary pointer value ... the original value ofs
is not modified.Furthermore, since you've incremented and changed the value of
s
with the++
operator, when you callcout
, 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 locations
is pointing to). Withs+1
, the value ofs
remains unmodified, so the result ofcout
will be to wherevers
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 froms++
is the original value ofs
. That means you'll end up with a stack overflow at some point and just crash unless the value thats
pointed to was already the NULL character.由于看起来 OP 将
print(s++)
更改为print(++s)
,这是巨大的不同,这里是这个新版本的解释。在第一个示例中,您有:
s+1 不修改 s。因此,如果 s 是 4,并且您
print(s+1)
,则之后 s 仍将是 4。在这种情况下,++s 会修改 s 的本地值。它将增加 1。因此,如果
print(++s)
之前是 4,那么之后将是 5。在这两种情况下,相当于 s+1 的值将被传递给 print 函数,使其打印下一个字符。
因此,这两个函数之间的区别在于,第一个函数将递归地打印字符 #0,然后是 1, 2, 3, ...,而第二个函数则打印 1, 2, 3, 4, ...(它跳过第一个字符,然后打印“\0”)。
示例:
对于
s+1
版本,print("hello")
将产生h
e
l
l
o
对于
++s
版本,print("hello")
将产生e
l
l
o
\0
Since it looks like the OP changed
print(s++)
toprint(++s)
, which is hugely different, here's an explanation for this new version.In the first example, you have:
s+1 does not modify s. So if s is 4, and you
print(s+1)
, afterwards s will still be 4.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 inh
e
l
l
o
For the
++s
version,print("hello")
will result ine
l
l
o
\0
表达式
s++
和s+1
都与增加指针本身的位置有关,而不是增加指针位置中包含的值< 的值code>s++ 只是
s
,而s+1
的值比s
更靠前一位!s++
后,s
的值比之前的位置移了一位。使用s+1
后,s
的值没有变化。因此,他们打印字母的顺序是相反的!
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
!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!
我将尝试解释一个前后增量的示例,您可以从中解决自己发布的问题。
输出:
为什么是10?
后自增操作是对数字进行的。意思是,number 的值首先传递给
foo
,然后 number 的值在foo
返回时递增。所以,函数返回后,number是11。为什么是12?
对number进行了预增操作。这意味着,在调用
foo
之前,number
的值会递增到 12。然后将其传递给foo
。因此,即使在函数返回之后,number 仍然是 12。为什么是 13?
这很简单。 number 的值没有被修改,而是传递了一个在 number 的值上加 1 的值。在此过程中,number 没有被修改。因此,即使在函数返回之后,number 仍然是 12。
希望这有助于您自己解决问题(尽管在您的情况下这是纸笔练习):)
I will try to explain an example of pre and post increment from which you can solve the question posted yourself.
Output:
Why 10?
Post-increment operation is done on number. Meaning, value of number is first passed to
foo
and then the value of number is incremented uponfoo
return. So, after function return, number is 11.Why 12?
Pre-increment operation is done on number. Meaning, before even call to
foo
, the value ofnumber
is incremented to 12. And then it is passed tofoo
. 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 ) :)