为int的增量值被指针指向 [英] increment value of int being pointed to by pointer
问题描述
我有一个int指针(INT *计数)如果我想递增整数正指向使用++我想我会叫
I have an int pointer (int *count) if i want to increment the integer being pointed at using ++ I thought I would call
*count++;
不过,我得到一个生成警告前pression结果未使用。我可以叫
However, I am getting a build warning "expression result unused". I can call
*count += 1;
不过,我想知道如何使用++。任何想法?
But, I would like to know how to use the ++. Any ideas?
推荐答案
该++都有平等的precedence与*和结合性的从右至左的。看到这里 。它变得更加复杂,因为即使++将与相关指针的增量是该语句的评估后应用。
The ++ has equal precedence with the * and the associativity is right-to-left. See here. It's made even more complex because even though the ++ will be associated with the pointer the increment is applied after the statement's evaluation.
事情发生的顺序是:
- 后递增,记住后递增的指针地址值作为临时
- 取消引用非递增的指针地址
- 应用递增的指针地址来算,现在算指向下一个可能的内存地址的类型的实体。
因为你从来没有真正在步骤2中一样@Sidarth说,你需要括号强制评估顺序使用提领值你得到警告:
You get the warning because you never actually use the dereferenced value at step 2. Like @Sidarth says, you'll need parenthesis to force the order of evaluation:
(*ptr)++
这篇关于为int的增量值被指针指向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!