I am trying to implement a sudoku solution. To do this, I use the structure shown below to represent a cell on a sudoku board. Then I declare a 9x9 array of these structures to represent the board.
Cell structure:
struct cell{
char value;
unsigned int possible;
};
then I declare the sctructs array as:
struct cell board[9][9];
My problem is that when I try to enter a value into an array (for example, board [2] [2] .value = getchar ();), sometimes it works, and sometimes I get this error:
Bus error: 10
I'm not quite sure what this means ... How is "Bus Error: 10" different from a segmentation error?
gcc vim. , . , malloc , :
int ** Array;
Array = (int**) malloc(x_size*sizeof(int*));
for (int i = 0; i < x_size; i++)
Array[i] = (int*) malloc(y_size*sizeof(int));
2- .
- ?
struct cell** board;
board = (struct cell**) malloc(x_size*sizeof(struct cell**));
for(int i=0; i< x_size; i++)
board[i] = (struct cell*) malloc(y_size*sizeof(struct cell));
, "sizeof (struct cell)" , .
! C (++ - ), C, .
!
!
, , , , , , :
/* only code being used in solver.h*/
29 /* structure to describe a cell */
30 struct cell{
31 int value;
32 unsigned int possible;
33 };
/*solver.c*/
4 #include <ctype.h>
5 #include <stdio.h>
6 #include "solver.h"
7
8
9 struct cell board [9][9];
10
11
12 int main(){
13 initialize_board();
14 print_board();
15 setup_board();
16 print_board();
17 return 0;
18 }
19
20 void print_board(){
21 int i=0, j=0;
22 for(i = 0; i<9; i++){
23 for(j = 0; j<9; j++)
24 printf(" %d",board[i][j].value);
25 printf("\n");
26 }
27 }
28
29 void initialize_board(){
30 int i = 0, j = 0;
31
32 for(i = 0; i<9; i++)
33 for(j = 0; j<9; j++){
34 (board[i][j]).value = 0;
35 (board[i][j]).possible = 0x1FF;
36 }
37 }
38
39 void setup_board(){
40 int row=0, col=0, val = 0;
41 char another = 'Y';
42
43 printf("Board Initial Setup.\nEnter the row and column number of the value to be entered into the board.");
44 printf("\nRow and Column indexes start at one, from top left corner.");
45 while(another == 'Y'){
46 printf("\nRow: ");
47 row = getchar();
48 printf("Column: ");
49 getchar();
50 col = getchar();
51 printf("Value: ");
52 getchar();
53 (board[row-1][[col-1]).value = getchar();
54 printf("Enter another value? (y/n): ");
55 getchar();
56 another = toupper(getchar());
57 getchar();
58 }
59 }
, int, getchar(). - / . , while setup_board say Row: 1, Col: 1, Value: 5, , "n", , 5 , . initialize_board().
:
Board Initial Setup.
Enter the row and column number of the value to be entered into the board.
Row and Column indexes start at one, from top left corner.
Row: 1
Column: 1
Value: 4
Enter another value? (y/n): n
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
, , :
:
Board Initial Setup.
Enter the row and column number of the value to be entered into the board.
Row and Column indexes start at one, from top left corner.
Row: 5
Column: 5
Value: 5
Bus error: 10
, getchar(), .
!
getchar()... , ASCII- . , :
47 while(another == 'Y'){
48 valid=0;
49 while(!valid){
50 printf("\nRow: ");
51 row = getchar() - '0';
52 getchar();
53 printf("Column: ");
54 col = getchar() - '0';
55 getchar();
56 printf("Value: ");
57 val = getchar() - '0';
58 getchar();
59 if(val >9 || val<1 || col>9 ||col<1 || row>9 || row<1)
61 printf("\nInvalid input, all values must be between 1 and 9, inclusive");
62 else
63 valid = 1;
64 }
65 board[row-1][col-1].value = val;
66 printf("Enter another value? (y/n): ");
67 another = toupper(getchar());
68 getchar();
69 }
, , , !
!
, - , ?
, , , , , ?