【问题标题】:While loops not working and crashing the console [duplicate]虽然循环不起作用并使控制台崩溃[重复]
【发布时间】:2018-02-23 22:43:31
【问题描述】:

每当我在 CodeBlocks 中启动此代码时,它都会使控制台崩溃并返回:

进程返回-1073741819 (0xC0000005)

代码:

#include <stdio.h>
#include <stdlib.h>

main() {
int next, prev, max;

while (1) {
    printf("Number: ");
    scanf("%d", next);
    if (next > prev) {
        prev = next;
        max = next;
    }
    if(next = 0){
        break;
    }
}
printf("Max number is: %d", max);

return 0;
}

【问题讨论】:

  • prev 未初始化

标签: c while-loop crash crash-reports


【解决方案1】:

改变

scanf("%d", next);

scanf("%d", &next); // note & operator

%d 说明符期望对应的参数是int * 类型的表达式,即指向int 的指针。您正在传递存储在next 中的(未初始化的、不确定的),这几乎肯定不是有效的指针值。

【讨论】:

  • 好吧,把我染成紫色,我完全错过了那个......
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多