创建connect 4 board c程序时出现分段错误

我正在创建一个连接4游戏……我已经做了很多事情; 然而,我创建我的电路板的方式是静态的,它需要是动态的,所以我已经制定了一个辅助程序来解决这个问题,然后在我的主程序中实现它。 出于某种原因,这段代码中的if&else-if条件会产生分段错误,我无法弄清楚为什么……

// for the rows/columns of the board for(row = num_rows - 1; row >= 0; row--){ printf("|"); for(col = 0; col < num_columns; col++){ if(aPtr[row][col] == '0') { printf("| X "); } else if(aPtr[row][col] == '1') { printf("| O "); } else { printf("| "); } } puts("||"); } 

当我评论这些条件时,电路板打印就好了,看起来像这样

 ------ Connect *Four ------ Connect X Command Line Game &&===================&& || | | | | || || | | | | || || | | | | || || | | | | || || | | | | || || | | | | || || | | | | || || | | | | || || | | | | || || | | | | || &&===================&& 1 2 3 4 5 

这个侧面程序的全部内容如下,任何关于为什么发生这种分段错误的见解都将受到赞赏。

  #include  #include  #include  #include  void initialize(int num_rows, int num_cols, char **aPtr) { int i, r, c; // create the space for the board aPtr = malloc(num_rows * sizeof(char*)); for (i = 0; i < num_rows; i++){ aPtr[i] = malloc(num_cols * sizeof (char)); } // go through the board and set all values equal to -1 for (r = 0; r < num_rows; r++) { for (c = 0; c < num_cols; c++) { aPtr[r][c] = '9'; printf("%c", aPtr[r][c]); } printf("\n"); } } void printBoard(int num_rows, int num_columns, char **aPtr) { int row, col; printf("\n"); puts("------ Connect *Four ------"); puts("Connect X Command Line Game"); // for fancy top of board frame printf("&&"); for(col = 1; col = 0; row--){ printf("|"); for(col = 0; col < num_columns; col++){ // if(aPtr[row][col] == '0') { // printf("| X "); // } // else if(aPtr[row][col] == '1') { // printf("| O "); // } // else { printf("| "); // } } puts("||"); } // for fancy bottom of board frame printf("&&"); for(col = 1; col < num_columns; col++) { printf("===="); } printf("==="); printf("&&\n"); printf(" "); if (col < 100){ for(col = 0; col < num_columns; col++) { if (col < 10) { printf(" %d ", col + 1); } else { printf("%d ", col + 1); } } puts("\n"); } } // ******************************************************************************************************* // ******************************************************************************************************* int main (int argc, char *argv[]) { char **aPtr; int height = 10; int width = 5; int i; initialize(height, width, aPtr); printBoard(height, width, aPtr); } 

这是对代码的修改,也许会有所帮助。 请注意,我正在传递&aPtr*aPtr = (char*) malloc(...)

 #include  #include  #include  #include  void initialize(int num_rows, int num_cols, char **aPtr) { int i, r, c; // create the space for the board *aPtr = (char*) malloc(num_rows * sizeof(char*)); if(*aPtr == NULL) { free(*aPtr); printf("Memory allocation failed"); } for (i = 0; i < num_rows; i++){ aPtr[i] = (char *) malloc(num_cols * sizeof (char)); } // go through the board and set all values equal to -1 for (r = 0; r < num_rows; r++) { for (c = 0; c < num_cols; c++) { aPtr[r][c] = '9'; printf("%c", aPtr[r][c]); } printf("\n"); } } void printBoard(int num_rows, int num_columns, char **aPtr) { int row, col; printf("\n"); puts("------ Connect *Four ------"); puts("Connect X Command Line Game"); // for fancy top of board frame printf("&&"); for(col = 1; col < num_columns; col++) { printf("===="); } printf("==="); printf("&&\n"); // for the rows/columns of the board for(row = num_rows - 1; row >= 0; row--){ printf("|"); for(col = 0; col < num_columns; col++){ if(aPtr[row][col] == '0') { printf("| X "); } else if(aPtr[row][col] == '1') { printf("| O "); } else { printf("| "); } } puts("||"); } // for fancy bottom of board frame printf("&&"); for(col = 1; col < num_columns; col++) { printf("===="); } printf("==="); printf("&&\n"); printf(" "); if (col < 100){ for(col = 0; col < num_columns; col++) { if (col < 10) { printf(" %d ", col + 1); } else { printf("%d ", col + 1); } } puts("\n"); } } // ******************************************************************************************************* // ******************************************************************************************************* int main (int argc, char *argv[]) { char *aPtr; int height = 10; int width = 5; int i; initialize(height, width, &aPtr); printBoard(height, width, &aPtr); } 

请注意,您正在执行: aPtr[r][c] = '9'; 所以你的董事会是空的,但如果你改变它说0你会得到类似的东西:

 ------ Connect *Four ------ Connect X Command Line Game &&===================&& || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || || X | X | X | X | X || &&===================&& 1 2 3 4 5 

我假设那是你的期望?