C++ 中的指针递增

发布于 2024-11-06 00:05:52 字数 235 浏览 0 评论 0原文

为什么下面的两个代码段不等价?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烏雲後面有陽光 2024-11-13 00:05:52

++ 运算符递增指针值,但随后返回原始值...因此 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 ... 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.

月朦胧 2024-11-13 00:05:52

由于看起来 OP 将 print(s++) 更改为 print(++s),这是巨大的不同,这里是这个新版本的解释。

在第一个示例中,您有:

print(s+1);
cout << *s;

s+1 不修改 s。因此,如果 s 是 4,并且您print(s+1),则之后 s 仍将是 4。

print(++s);
cout << *s;

在这种情况下,++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++) 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

度的依靠╰つ 2024-11-13 00:05:52
  1. 表达式s++s+1都与增加指针本身的位置有关,而不是增加指针位置中包含的值

  2. < 的值code>s++ 只是 s,而 s+1 的值比 s 更靠前一位!

  3. 执行s++后,s的值比之前的位置移了一位。使用s+1后,s的值没有变化。

因此,他们打印字母的顺序是相反的!

  1. Both of the expressions s++ and s+1 are to do with increasing the position of the pointer itself, not the value contained at the pointer locations

  2. The value of s++ is just s, and the value of s+1 is, well, one position further on than s!

  3. The value of s after executing s++ is one position further on than it was before. After using s+1, the value of s is unchanged.

Therefore the order they print out the letters is reversed!

月竹挽风 2024-11-13 00:05:52

我将尝试解释一个前后增量的示例,您可以从中解决自己发布的问题。

#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 ;
}

输出:

10
12
13

为什么是10?

foo( number++ ) ;

后自增操作是对数字进行的。意思是,number 的值首先传递给 foo,然后 number 的值在 foo 返回时递增。所以,函数返回后,number是11。

为什么是12?

foo( ++number ) ;

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.

#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 ) :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文