指针所指向的 int 的增量值
我有一个 int
指针(即 int *count
),我想使用 ++
运算符来增加所指向的整数。我想我会打电话:
*count++;
但是,我收到了构建警告“表达式结果未使用”。我可以: call
*count += 1;
但是,我也想知道如何使用 ++
运算符。有什么想法吗?
I have an int
pointer (i.e., int *count
) that I want to increment the integer being pointed at by using the ++
operator. I thought I would call:
*count++;
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 ++
operator as well. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
++ 与 * 具有相同的优先级,并且结合性是从右到左。请参阅此处。 它变得更加复杂,因为即使 ++ 将与指针增量在语句评估后应用。
事情发生的顺序是:
您收到警告是因为您在步骤 2 中从未实际使用过取消引用的值。就像 @Sidarth 所说,您需要括号来强制执行计算顺序:
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.
The order things happen is:
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:
尝试使用
(*count)++
。 *count++ 可能会将指针递增到下一个位置,然后使用间接寻址(这是无意的)。Try using
(*count)++
.*count++
might be incrementing the pointer to next position and then using indirection (which is unintentional).