我在編譯在Visual Studio 2013編寫了一個程序,我得到這個錯誤: ((在0x5837FB53(msvcr120d.dll)在ConsoleApplication2.exe未處理的異常:0000005:訪問衝突讀取位置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`
看起來像一個假地址('0x0000006D')??? –
您尚未預留NUL終結符的空間。掃描'%s'時不要在變量名前使用'&'。如果(th <0);'應該是'if(th <0)' –
當'i> = 2'時不要訪問'str1 [i]'和'str2 [i]'。代碼'str1 [i] = str3 [i] - '0';'可以做那個超出範圍的訪問。 – MikeCAT