C++ 递增指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11754419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:30:27  来源:igfitidea点击:

Incrementing Pointers

c++pointersincrement

提问by Mohamed Ahmed Nabil

I have a question about incrementing in pointers that I dont quite understand.

我有一个关于递增指针的问题,我不太明白。

Lets see 2 small programs:

来看2个小程序:

int iTuna=1;
int* pPointer= &iTuna;
*pPointer = *pPointer + 1 ; //Increment what pPointer is pointing to.
cout << iTuna << endl;

In this first program I increment what pPointer is pointing to like this "*pPointer = *pPointer +1". And as I expected iTuna changed to "2" and the program printed out the value "2"

在第一个程序中,我增加了 pPointer 指向的内容,就像这样“*pPointer = *pPointer +1”。正如我所料,iTuna 更改为“2”并且程序打印出值“2”

int iTuna=1;
int* pPointer= &iTuna;
*pPointer++; //Increment what pPointer is pointing to.
cout << iTuna << endl;
system("PAUSE");
return 0;

Here I incremented incremented what pPointer is pointing to this was "*pPointer++". But here iTuna stays as "1" and the programs prints out the value "1" . Although I expected this one to work as the first, it didn't.

在这里,我增加了 pPointer 指向的内容是“*pPointer++”。但是这里 iTuna 保持为 "1" 并且程序打印出值 "1" 。虽然我希望这个可以作为第一个工作,但它没有。

Please Help me and tell me why the second peice of code isn't working like I expected and how to get around it.

请帮助我并告诉我为什么第二段代码没有像我预期的那样工作以及如何解决它。

Thank You

谢谢你

回答by Michael Chinen

*pPointer++;

is equivalent to

相当于

*pPointer;
pPointer++; 

so it increments the pointer, not the dereferenced value.

所以它增加了指针,而不是取消引用的值。

You may see this from time to time in string copy implementations like

您可能会不时在字符串复制实现中看到这一点,例如

  while(*source)
    *target++ = *source++;

Since your problem is a matter of operator precedence, if you want to deref the pointer, and then increment, you can use parens:

由于您的问题是运算符优先级的问题,如果您想取消引用指针,然后递增,则可以使用括号:

(*pointer)++;

回答by texasbruce

++ operator precedence is higher than *d dereference.

++ 运算符优先级高于 *d 取消引用。

What you write is actually

你写的其实是

*(p++)

However you should use

但是你应该使用

(*p)++

回答by Andrew

 *ptr++; - increment pointer and dereference old pointer value

It's equivalent to:

它相当于:

*(ptr_p++) - increment pointer and dereference old pointer value

Here is how increment the value

这是增加值的方法

(*ptr)++; - increment value

That's becuase ++has greater precedence than *, but you can control the precedence using ()

那是因为++比 具有更高的优先级*,但您可以使用控制优先级()

回答by AArora

In the Second program you are not increasing the the content at the pPointer address, but you are increasing the pointer. So suppose here if the pPointer value(memmory location allocated to iTuna) is 1000 then it will increase the location to 1000+2(int size)=1002 not the content to 1+1=2. And In the above program you are accessing the pointer location contents. Thats why you are not getting the expected results

在第二个程序中,您没有增加 pPointer 地址处的内容,而是增加了指针。所以假设这里如果 pPointer 值(分配给 iTuna 的内存位置)是 1000,那么它会将位置增加到 1000+2(int size)=1002 而不是内容增加到 1+1=2。在上面的程序中,您正在访问指针位置内容。这就是为什么你没有得到预期的结果

回答by rashok

*pPointer++;- Here dereference operator(*) has more precedence than increment operator(++). So this statement is first dereferencing and incrementing the pointer. After this you are printing the value of iTunawhich will give you the same value. You are not printing the value by dereferencing pointer variable(*pPointer), because this will leads to crash(undefined behaviour). Because pPointeris now incremented.

*pPointer++;- 这里解引用 operator( *) 比增量 operator( ++)具有更高的优先级。所以这个语句首先取消引用并增加指针。在此之后,您打印的值iTuna将为您提供相同的值。您不是通过取消引用指针变量(*pPointer)来打印值,因为这会导致崩溃(未定义的行为)。因为pPointer现在增加了。

Use like (*pPointer)++;to increment the value which is pointed by pPointer.

使用 like(*pPointer)++;来增加 指向的值pPointer

To get clear idea print the address stored in pPointervariable before and after your increment statement.

要获得清晰的想法pPointer,请在增量语句之前和之后打印存储在变量中的地址。

回答by Lo?ch

In the first case, the content of the pointer is incremented because *pPointer correspond to the content of the variable iTuna.

在第一种情况下,指针的内容会增加,因为 *pPointer 对应于变量 iTuna 的内容。

In the second one, the content is not incremented because you pPointer incrementing the pointer address. Remembering operator precedence rules, postfix operators such as increment (++) and decrement (--), have higher precedence than prefix operators, such as the dereference operator (*). Therefore, writting

在第二个中,内容没有增加,因为您 pPointer 增加了指针地址。记住运算符优先级规则,后缀运算符如递增 (++) 和递减 (--) 的优先级高于前缀运算符,如解引用运算符 (*)。因此,写

*pPointer++

*pPointer++

is equivalent to

相当于

*(pPointer++)

*(pPointer++)

And what it does is to increase the value of pPoiner (so it now points to the next element), but because ++ is used as postfix, the whole expression is evaluated as the value pointed originally by the pointer (the address it pointed to before being incremented).

它所做的是增加 pPoiner 的值(所以它现在指向下一个元素),但是因为 ++ 用作后缀,所以整个表达式被评估为指针最初指向的值(它指向的地址)在增加之前)。

The correct code to have what you are expecting for is the following:

拥有您所期望的正确代码如下:

++*pPointer

++*pPointer

or

或者

(*pPointer)++

(*pPointer)++