分段错误:C代码中的11
为什么我的代码有分段错误分段错误:C代码中的11,c,segmentation-fault,C,Segmentation Fault,为什么我的代码有分段错误 /* solver.h header file */ 10 struct options{ 11 unsigned int one:1, two:1, three:1, four:1, five:1, six:1, seven:1, eight:1, nine:1; 12 }; 13 14 15 /* structure to describe a cell */ 16 struct cell{ 17 short value; 1
/* solver.h header file */
10 struct options{
11 unsigned int one:1, two:1, three:1, four:1, five:1, six:1, seven:1, eight:1, nine:1;
12 };
13
14
15 /* structure to describe a cell */
16 struct cell{
17 short value;
18 struct options open_options;
19 };
c.c:
5 #include <stdio.h>
6 #include "solver.h"
7
8
9
10
11
12 int main(){
13 struct cell board [9][9];
14 int i=0,j=0;
15
16
17 for(i = 1; i<10; i++)
18 for(j = 1; j<10; j++)
19 (board[i][j]).value = j;
20
21 for(i = 1; i<10; i++){
22 for(j = 1; j<10; j++)
23 printf(" %d",(board[i][j]).value);
24 printf("\n");
25 }
26 return 0;
27 }
5#包括
6#包括“solver.h”
7.
8.
9
10
11
12 int main(){
13结构单元板[9][9];
14 int i=0,j=0;
15
16
17 for(i=1;i数组的索引从0开始,因此循环应该是for(i=0;i某些类型的数组[9];
将数组定义为一个包含9个元素的数组,下标从0到8都包含在内。您不能使用数组[9]
板[9][9]
将包含索引范围为0…8,而不是1…9的元素。当您分配到板[9][任何内容]
,实际上您重写了不属于您的内存,当返回0
将控制权交回C运行时,它开始遍历其结构以执行关闭操作时,这恰好导致程序爆炸。您可以使用gdb
C数组中的第一个元素x实际上是(i=0;i<9;i++){}
BTW:如果你打算实现数独解算器:位掩码将更容易,因为你可以在一次扫描中屏蔽所有数字。@wildplasser是的,我现在看到了。我对C还是相当陌生;我想一个位域也可以被类似的屏蔽。但是谢谢你!每个单元格的位掩码不如每行/列/框的位掩码有用。不要这样做Don’别忘了,有时候你必须回头(在你摘下低垂的果实之后)