C 中取消引用的 int* 和 int 的布尔比较失败
我正在 Debian Linux x64 上用 C 编写程序。在部分代码中,我有以下 if 语句:
if (*all_drops >= (npes * 128)) {
break;
}
all_drops
是一个 int*
,它已被分配了 int
的内存价值,而 npes
是一个 int
,值为 2。因此,当 all_drops
中的值至少为 256 时,if 语句应该为 true。然而,我已经让变量超过 1000,而 if 语句的计算结果为 true。
我不确定将取消引用的指针与其他数字进行比较是否存在一些细微差别,但我搜索过 Google,但没有找到任何类似的问题(这让我相信我错过了一些东西)。如果您能提供任何帮助,我们将不胜感激。
I'm writing a program in C on Debian Linux x64. In part of the code, I have the following if statement:
if (*all_drops >= (npes * 128)) {
break;
}
all_drops
is an int*
which has been assigned an int
's worth of memory, while npes
is an int
with the value 2. Therefore, the if statement should be true when the value in all_drops
is at least 256. However, I've had the variable get above 1000 without the if statement evaluating true.
I'm not sure if there's some nuance in comparing dereferenced pointers to other numbers, but I've searched Google and haven't been able to find any similar issues (which leads me to believe I am missing something). Any help you could give would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这按预期工作:
http://ideone.com/DjHGI
我想问题可能出在你初始化的方式
*all_drops
This works as intented:
http://ideone.com/DjHGI
I guess the problem can be in the way you're initializing
*all_drops