首页 > 解决方案 > 线程 1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT) 导致我的 scanf 失败

问题描述

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, const char * argv[]) {
    bool isTrue=true;
    int nums[50];
    for (int i = 0; i<sizeof(nums); i++) {
        nums[i]=2147483647;
    }
    char operations[49];
    int counter = 0;
    printf("What is your first number? (THERE IS NO PEMDAS, THE NUMBER 2147483647 is not allowed)\n");
    scanf("%d", &nums[counter]); // Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
}

我有更多代码,但这与问题无关。感谢您的帮助,并询问是否需要更多信息(我正在制作一个计算器,它可以排列输入并计算它们,例如 3 + 5 * 9 / 2 = 36。这个方程最多可以有 50 个整数和 49 个操作。)

标签: cintscanfcalculatorexc-bad-access

解决方案


您以字节为单位计算数组的大小,而不是元素的数量。改为这样做:

for( int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++ )
{

推荐阅读