c - 线程 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 个操作。)
解决方案
您以字节为单位计算数组的大小,而不是元素的数量。改为这样做:
for( int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++ )
{
推荐阅读
- json - Jsonata 通过查看父元素过滤值
- javascript - 如何使用传单插件(没有 vue2 包装器)将 Vue-Leaflet 中的地图显示限制在特定边界
- kubernetes - 使用单个 HELM 模板的多个资源
- android - AsyncTask 仅在较新的 Android 上返回 null
- python - 如何动态创建线程来执行 Web 请求?
- c# - 在 C# 中,如何检查是否在两次之间?
- android - 在不旋转应用程序的情况下检测 Ionic 4 中的设备旋转/方向
- docker-compose - 无法用pycharm远程启动项目
- java - Spring 容器实例化、配置和组装对象
- tcp - TCP 推送数据包未从 tun 传递