我对指针有问题。我尝试将板子(双指针)设置为gameboard->Board
,并且在尝试打印时函数外部出现怪异的符号,因此我认为我做错了。
我将不胜感激能帮助您理解做这种事情的正确方法。
这里是代码:
typedef struct{
int value;
char sym;
}Cell;
typedef struct{
int BoardSize;
int BlockWidth;
int BlockHeight;
int erroneous;/*0- no error, 1- error*/
Cell** Board;
}GameBoard;
void make9X9Board(GameBoard *gameboard){/*init to an empty 9X9 board*/
int i;
int j;
Cell **board=(Cell**) malloc(sizeof(Cell)*9*9);/*allocate memory for the board*/
for (i = 0; i < 9; i++) {
board[i] = (Cell *) malloc(sizeof(Cell) * 9);
}
/*make an empty board*/
for(i=0;i<9;i++){
for(j=0;j<9;j++){
board[i][j].value=0;
board[i][i].sym=' ';
}
}
/*free previous board*/
for (i = 0; i < gameboard->BoardSize; i++) {
free(gameboard->Board[i]);
}
free(gameboard->Board);
/*init new gameboard values*/
gameboard->BlockHeight=3;
gameboard->BlockWidth=3;
gameboard->BoardSize=9;
gameboard->erroneous=0;
gameboard->Board=board;
}
void edit(GameBoard *gameboard,Mode *mode, char *x, int isX, int MarkE){/*load a board to edit mode, if isX=0 we didn't get a path, else we did */
if(isX==0){/*means the function works without path to file so make 9X9 empty board */
make9X9Board(gameboard);
*mode=EDIT_M;
print_board(gameboard,*mode,MarkE);
return;
}
/*else we got a path so work with it so just use solve with isEdit=1 for edit special cases*/
solve(gameboard,mode,x,MarkE,1);
*mode=EDIT_M;
print_board(gameboard,*mode,MarkE);
}
int main(){
Mode *mode=(Mode*)malloc(sizeof(Mode));
char *x;
int i;
GameBoard* board;
board=(GameBoard*) malloc(sizeof(GameBoard));/*further initialization when loading a certain board*/
board->BlockHeight=3;
board->BlockWidth=3;
board->BoardSize=9;
board->Board=(Cell**) malloc(sizeof(Cell)*9*(9));/*allocate memory for the board*/
for (i = 0; i < 9; i++) {
board->Board[i] = (Cell *) malloc(sizeof(Cell) * 9);
}
x="D:/Users/x.txt";
*mode=INIT_M;
edit(board,mode,x,0,0);
print_board(board,*mode,0);
}
这不是一个答案,这里仅是在说明中做更多说明
从您的代码中,如果我加上:
typedef enum Mode { INIT_M, EDIT_M } Mode;
void print_board(GameBoard *gameboard,Mode mode, int MarkE)
{
printf("%d %d %d %d\n",
gameboard->BoardSize, gameboard->BlockWidth,
gameboard->BlockHeight, gameboard->erroneous);
int i, j;
Cell ** b = gameboard->Board;
for (i = 0; i != gameboard->BlockHeight; i += 1) {
for (j = 0; j != gameboard->BlockWidth; j += 1)
printf("[%d '%c'] ", b[i][j].value, b[i][j].sym);
putchar('\n');
}
}
void solve(GameBoard *gameboard,Mode* mode, char *x, int MarkE, int dummy)
{
}
能够进行编译和执行没有明显的问题:
bruno@bruno-XPS-8300:/tmp$ ./a.out
9 3 3 0
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
9 3 3 0
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
bruno@bruno-XPS-8300:/tmp$
以及valgrind下:
bruno@bruno-XPS-8300:/tmp$ valgrind ./a.out
==14595== Memcheck, a memory error detector
==14595== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==14595== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==14595== Command: ./a.out
==14595==
9 3 3 0
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
9 3 3 0
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
[0 ' '] [0 ' '] [0 ' ']
==14595==
==14595== HEAP SUMMARY:
==14595== in use at exit: 1,324 bytes in 12 blocks
==14595== total heap usage: 23 allocs, 11 frees, 3,644 bytes allocated
==14595==
==14595== LEAK SUMMARY:
==14595== definitely lost: 28 bytes in 2 blocks
==14595== indirectly lost: 1,296 bytes in 10 blocks
==14595== possibly lost: 0 bytes in 0 blocks
==14595== still reachable: 0 bytes in 0 blocks
==14595== suppressed: 0 bytes in 0 blocks
==14595== Rerun with --leak-check=full to see details of leaked memory
==14595==
==14595== For counts of detected and suppressed errors, rerun with: -v
==14595== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
bruno@bruno-XPS-8300:/tmp$
所以问题不在您提供给我们的代码中