我收到错误消息"SIGSEGV on thread“。我怎么才能修复它?
#include <stdio.h>
int n[10], n2[10], num, nun, i=10, j=10, sv=0, on, res[4][20], opc[4][20];
main(){
printf("Insert the 1st number: ");
scanf("%d", &num);
sv = num;
while (num != 0)
{
n[i] = num%10;
num = num/10;
i--;
}
num = sv;
printf("Insert the 2nd number: ");
scanf("%d", &nun);
sv = nun;
while (nun != 0)
{
n2[j] = nun%10;
nun = nun/10;
j--;
}
nun = sv;
printf("Select an operation (1=addition; 2=subtraction; 3=multiplication; 4=division): ");
scanf("%d", &on);
while((on>4)||(on<0)){
printf("Try again: ");
scanf("%d", &on);
}
for (j=1; j<=8; j++){
for (i=20; i>=1; i--){
opc[j][i]=0;
}
}
if (on==1)
{
for (i=10; i>=1; i++)
{
if ((n[i] + n2[i] + opc[1][10+i]) <= 9)
{
opc[1][10+i] += (n[i] + n2[i]);
}
if ((n[i] + n2[i] + opc[1][10+i]) > 9)
{
opc[1][10+i] += (n[i] + n2[i])%10;
opc[1][9+i] += (n[i] + n2[i])/10;
}
}
}
else
{
printf("Coming soon :)");
}
}发布于 2018-06-19 07:01:32
对于大小为10的数组,n10不是第一个循环中的有效下标。最后一个有效下标比数组的大小小一(因此为0- 9)。您将从n9开始并向下递减到n,因此将n和j更改为9:
n = 9;
j = 9;这同样适用于你的二维数组。每个维度的最后一个有效下标是比大小(行/列)小一。opc8不是有效的下标,这是for循环将尝试访问的下标。
https://stackoverflow.com/questions/50918467
复制相似问题