gpt4 book ai didi

c - "SIGSEGV on thread"错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:47 24 4
gpt4 key购买 nike

我收到错误“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 :)");
}
}

最佳答案

n[10] 不是大小为 10 的数组的第一个循环中的有效下标。最后一个有效下标比数组的大小小一个(因此为 0 - 9)。您将从 n[9](而不是 10)开始并递减到 n[0],因此将 n 和 j 更改为 9:

n = 9;
j = 9;

二维数组也是如此。每个维度的最后一个有效下标比大小(行/列)小一个。 opc[8][20] 不是有效的下标,您的 for 循环将尝试访问该下标。

关于c - "SIGSEGV on thread"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50918467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com