2017-03-04 63 views
0

我正在创建一个connect-4游戏......我做了很多事情;然而,我创建我的主板的方式是静态的&它需要是动态的,所以我已经做了一个副程序来解决这个问题,然后在我的主程序中实现它。出于某种原因,如果&否则,如果在此块的代码创建条件语句分段错误,我想不通为什么...创建连接时的分段错误4 board c程序

// 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 <stdio.h> 
      #include <stdlib.h> 
      #include <string.h> 
      #include <sys/stat.h> 


      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 < 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); 
      } 
+1

'aPtr = ...'不能更新调用者端变量。 – BLUEPIXY

+0

@BLUEPIXY你指的是哪一行代码? –

+0

'aPtr = malloc(num_rows * sizeof(char *));' – BLUEPIXY

回答

0

这是您的代码的修改,也许它会有所帮助。请注意,我路过&aPtr*aPtr = (char*) malloc(...)

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/stat.h> 


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';所以你的主板是空的,但如果你改变它说0you会得到这样的:

------ 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 

我假设这就是你的预期?