C 在三维数组上迭代
我的代码中不断出现总线错误10,我不知道为什么C 在三维数组上迭代,c,arrays,sum,sequence,bus,C,Arrays,Sum,Sequence,Bus,我的代码中不断出现总线错误10,我不知道为什么 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define SIZE 100 int main() { char input[SIZE]; int dummy[SIZE]; int array[SIZE][SIZE][SIZE]; int set=-1; int sequence=0;
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 100
int main() {
char input[SIZE];
int dummy[SIZE];
int array[SIZE][SIZE][SIZE];
int set=-1;
int sequence=0;
while (1==1) {
if (fgets(input, SIZE-1, stdin) == NULL){
printf("Input Error.");
break;
}else {
char* s;
for (s = input; (*s != '\n') && isspace(*s); s++){
;
}
if (*s == 'f'){ //start of finish
break;
} else if (*s == 'S'){ //start of SET
set++;
} else{
sscanf(input, "%d: %d, %d, %d, %d, %d", &dummy[0],
&dummy[1], &dummy[2], &dummy[3], &dummy[4],
&dummy[5]);
array[set][sequence][0]=dummy[0];
array[set][sequence][1]=
dummy[1]+dummy[2]+dummy[3]+dummy[4]+dummy[5];
sequence++;
}
}
}
for (int i=0; i<sizeof(array); i++ ){
printf("SET %d\n", i+1);
for (int j=0; j<sequence;j++){
printf("%d", array[i][j][0]);
for (int k=0; k<=5; k++){
printf("%d\n", array[i][j][1]);
}
}
}
return 0;
}
我想输出序列号和给定集合中每个序列的冒号后所有数字的总和。所以看起来像这样
SET 1
1 21
2 17
3 18
SET 2
1 27
2 21
但是,当我试图执行我的代码时,我得到了总线错误:10。我尝试过调整数组的大小,但现在我无法迭代数组,因为它会不断返回一组随机数。比如:
-34618613832766
32766
32766
32766
32766
32766
1178023731825050672
825050672
825050672
这些数字来自哪里?谢谢。您的阵列对于堆栈来说很可能太大了。有关如何增加堆栈大小或使用malloc在免费存储区aka堆上动态分配堆栈大小的信息,请参阅编译器手册。100000*100000*100000是一个巨大的数字,不是吗?为什么不开始测试一个小得多的数字,比如说100?我尝试将大小设置为1000,但却遇到了分段错误11,这让我更加困惑。假设sizeofint=2,数组的大小就像2*910 TB。你必须有一台大机器。你不能在堆栈上分配数千TB的数据,就这么简单。没有其他需要说的。
-34618613832766
32766
32766
32766
32766
32766
1178023731825050672
825050672
825050672