c - 未处理的异常

标签 c compilation

我在 Visual Studio 2013 中编写了一个程序,在编译期间出现此错误: ((ConsoleApplication2.exe 中 0x5837FB53 (msvcr120d.dll) 处未处理的异常:0xC0000005:读取位置 0x0000006D 时发生访问冲突。))

我该怎么办?

这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int jabs(int th)
{
    if (th < 0); {
        th *= -1;
    }

    return th;
}

int main()
{
    char depature[8][8] = { "08:00 am" ,"09:43 am","11:19 am","12:47 am", "02:00pm" ,"03:45 pm","07:00 pm","09:45 pm" };
    char arrival[8][8] = { "10:16 am", "11:52 am", "01:31 pm", "03:00 pm", "04:08 pm", "05:55 pm", "09:20 pm", "11:58 pm" };
    int dep[8] = { 800,943,1119,1247,1400,1545,1900,2145};
    int str1[2];
    int str2[2];
    char str3[10];
    int temp;
    int index;
    int a[10];

    int i;
    int j=0;
    int javab = 0;
    int javabs;
    printf("enter a time corresponding 24 hour");
    scanf("%s", &str3);
    for (i = 0; i <= 4; i++)
    {
        if (str3[i] != ':'){
            str1[i] = str3[i] - '0';

        }
        else 
        {
            i++;
            str2[j] = str3[i]-'0';
            i++;
            str2[j+1] = str3[i] - '0';
            break;
        }


    }
    str1[0] = str1[0] * 1000;
    str1[1] = str1[1] * 100;
    str2[0] = str2[0] * 10;
    javab = str1[0] + str1[1] + str2[0] + str2[1];
    for (j = 0; j < 8; j++)
        javabs = (javab - dep[j]);
        a[j] = jabs(javabs);
    temp = a[0];
    for (j = 1; j < 8; j++)
    {

        if (temp > a[j])
        {
            temp = a[j];
        }
    }   
    for (j = 0; j < 8; j++)
    {
        if (temp == a[j])
        {
            index = j;
            break;
        }

    }
    printf("closest departure time is %s,arriva at %s \n",depature[j][7],arrival[j][7]);
    return 0;

}`enter code here`

最佳答案

depature[j][7]arrival[j][7] 作为指针无效,因此您不得将它们传递给 %s printf 中。

线

printf("closest departure time is %s,arriva at %s \n",depature[j][7],arrival[j][7]);

应该是

printf("closest departure time is %s,arriva at %s \n",depature[j],arrival[j]);

这将使您避免出现段错误。

请注意,有more warnings :

prog.c: In function 'jabs':
prog.c:6:16: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
     if (th < 0); {
                ^
prog.c: In function 'main':
prog.c:30:5: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
     scanf("%s", &str3);
     ^
prog.c:22:9: warning: variable 'index' set but not used [-Wunused-but-set-variable]
     int index;

关于c - 未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33172242/

相关文章:

用作数组安全包装的结构体的 C++ 性能

c++ - 作为集成商,如何根据症状避免重复出现的代码错误?

c - 如何通过单个 Makefile 使用不同的标志组合自动编译代码

c++ - 将 header 添加到 CMake 中的库

java - 在java中,这样的类类型编译成什么?

c - 在 C 中绘制表格——就像 Linux 手册页上的表格

c - 循环终止且没有任何错误

基于短路逻辑运算的条件执行

generics - Ada 通用程序

在 OSX 上为 Shewchuk 的三角程序编译 C 浮点运算