c - Output prediction cannot understand the while statement
问题描述
I'm looking for the expalanation of this problem. I cannot understand the while part and why does it print 6.
#include <stdio.h>
#include<stdlib.h>
int main()
{
int array[] = {1, 2, 4, 0, 4, 0, 3};
int *p, sum = 0;
p = &array[0];
while (*p++)
sum += *p;
printf("%d\n", sum);
return 0;
}
解决方案
这是您的 while 循环的更易读的形式:
while (*p != 0)
{
p = p + 1; // you can put "p++;" here, which has the same effect
sum += *p;
}
现在你应该自己理解了。
推荐阅读
- apache-storm - 为什么storm使用XOR来确保拓扑中的每个Bolts都成功执行。而不是计数器
- scala - 未找到 Scala 反射
- angular - Reactive Form 在有效的情况下将一个新的 FormGroup 添加到 FormArray 时说它是无效的
- spring-boot - Spring批处理,直接运行job后检查jobExecution状态?
- python-3.x - python3 - 列表方法 - 变量/列表相等和在此相等后修改的列表
- python - Tkinter 主属性及其可能的用途
- javascript - React BaseTable 仅显示部分长文本(显示更多选项)
- spring-boot - 为什么应用程序第一次没有启动,而是从第二次启动?
- maven - Maven 无法识别 IntelliJ 项目结构中添加的依赖项
- google-chrome-extension - 当我尝试加载 Google 扩展程序时未显示它