【问题标题】:segmentation fault when creating connect 4 board c program创建连接 4 板 c 程序时出现分段错误
【发布时间】:2017-07-24 06:24:27
【问题描述】:

我正在创建一个 connect-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 <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);
            }

【问题讨论】:

  • aPtr = ... 无法更新调用方变量。
  • @BLUEPIXY 你指的是哪一行代码?
  • aPtr = malloc(num_rows * sizeof(char*));
  • @BLUEPIXY 我不确定我是否遵循...这不会给我任何警告或错误,并且初始化方法工作得很好。
  • DEMO

标签: c pointers multidimensional-array segmentation-fault


【解决方案1】:

这里是你的代码的修改,也许它会有所帮助。请注意,我正在传递 &amp;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';,所以你的板子是空的,但如果你把它改成 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  

我假设这就是您的预期?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多