创建Connect 4 Board C程序时出现分段错误 [英] segmentation fault when creating connect 4 board c program

查看:87
本文介绍了创建Connect 4 Board C程序时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个connect-4游戏……我已经完成了很多工作;但是,我创建董事会的方式是静态的& ;;它必须是动态的,因此在我在主程序中实现它之前,我制作了一个边程序来修复它.由于某些原因,if& else-如果这部分代码中的条件会产生分段错误,而我不知道为什么...

I'm creating a connect-4 game... I have a lot of it done; however, the way I was creating my board was static & it needed to be dynamic, so I've made a side program to fix this before implementing it in my main program. For some reason, the if & else-if conditionals in this chunk of code create a segmentation fault, and I can't figure out why...

  // 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("||");
   }

当我评论这些条件时,董事会的印刷品就可以了&看起来像这样

when I comment these conditionals out the board prints just fine & looks like this

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

下面是这个副程序的全部,对于为什么出现这种分段错误的任何见解,我们将不胜感激.

the entirety of this side-program is below, any insight as to why this segmentation fault is occurring will be appreciated.

    #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*aPtr = (char*) malloc(...)

Here is the modification of your code, maybe it will help. Note that I'm passing &aPtr and *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,您将得到类似的东西:

Note that you are doing: aPtr[r][c] = '9'; so your board is empty, but if you change it to say 0you would get something like:

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

我假设这就是您所期望的?

I'm assuming that's what you expected?

这篇关于创建Connect 4 Board C程序时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆