【问题标题】:How to initialize a 3d array in C - Array of arrays of pointers如何在 C 中初始化 3d 数组 - 指针数组
【发布时间】:2015-05-05 19:39:16
【问题描述】:

我正在编写一个游戏来生成下一个可能的动作。我需要生成下一步动作才能执行搜索。但是我不知道如何在 C 中做到这一点。

生成板子的代码是:

#include <stdio.h> //prints 
#include <stdbool.h> //bool
#include <stdlib.h>  //malloc 

static const int BOARD_SIZE = 6;
typedef int **BOARD;

void print_board(BOARD b){
    int i,j;
    printf("BOARD array is:\n");
    for (i=0; i<BOARD_SIZE; i++) {
        for (j=0; j<BOARD_SIZE; j++){
            printf("%d ",b[i][j]);
        }
        printf("\n");
    }
}

BOARD set_game(){
//set board
//all the squares starts with 2 
    int i, j;

    BOARD b = malloc(sizeof(int *) * BOARD_SIZE);
    for (i = 0; i < BOARD_SIZE; i++){
        b[i] = malloc(sizeof(int) * BOARD_SIZE);
    }

    for (i=0; i<BOARD_SIZE; i++) {
        for (j=0; j<BOARD_SIZE; j++){
            //position player 0 peons
            if(j == 0){
                b[i][j] = 0;
            }
            //position player 1 peons
            else if(j == BOARD_SIZE-1){
                b[i][j] = 1;
            }else{
                b[i][j] = 2;
            }
        }
    }

    print_board(b);
    return b;
}

// Game
int main(){
// a pointer to an int.
    BOARD p, board;
    p = set_game();

    board = board_status(p);
    return 0;

}

打印出来:

BOARD array is:
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1

我现在需要创建一个数组数组,以生成所有下一个可能的棋盘,例如,当玩家 0 从 b[0][0] 移动到 b[0][1] 时,这是一个叶子的分支。

BOARD array is:
2 0 2 2 2 1
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1

我应该如何分配这个数组? 我需要一个包含所有其他 board_status 的分支数组,然后我将执行我的搜索。 我不确定数组的类型,如何声明它?这将是一个BOARD数组?

我尝试使用这种方法,我找到了here,但似乎有问题。它给了我一个错误:

从“int”类型数组[i] = b[i][j]分配给“分支”类型时不兼容的类型;

//generate ALL possible moves
void generatePossibleMoves(int player){
int i, j;
typedef struct
{
    int BOARD[BOARD_SIZE];
} branches;

branches** array = NULL;

void InitBranches( int num_elements )
{
    array = malloc( sizeof( branches ) * num_elements);

    if( !array )
    {
       printf( "error\n" );
       exit( -1 );
    }

    for(i = 0; i < num_elements; i++ )
    {
        for(j = 0; j < BOARD_SIZE; j++ )
        {
           BOARD b = set_game();
           array[i] = b[i][j];
           printf("%d", array[i]);
        }
        printf("\n");
    }
}

InitBranches(4);


}

有人可以帮帮我吗?谢谢。

【问题讨论】:

    标签: c arrays pointers malloc


    【解决方案1】:

    你不应该在函数中有函数,将InitBranches移出generatePossibleMovestypedef struct 也应该在函数之外。

    您的array 声明与malloc 不匹配,您应该在声明中删除* 或在sizeof 中添加一个。

    只是猜测你想做什么:

    BOARD* InitBranches( int num_elements )
    {
        int i;
        BOARD* array = malloc(num_elements * sizeof *array);
    
        if( !array )
        {
            printf( "error\n" );
            exit( -1 );
        }
    
        for(i = 0; i < num_elements; i++ )
        {
           array[i] = set_game();
        }
        return array;
    }
    
    void generatePossibleMoves(int player){
    
        BOARD* array = InitBranches(4);
    
        //do your moves here
    }
    

    这将创建 4 个董事会分支 array[0] 直到 array[3]

    【讨论】:

    • 非常感谢。我对指针和 typedef 感到困惑。你知道如果我没有正确的叶子数量并且想通过执行时间来限制分支的深度,我怎样才能在没有它的大小的情况下初始化一个板数组?非常感谢!
    • 你可以reallocarray每次你想添加一个分支(如果太小最好加倍大小)。如果您想在示例中添加第 5 个分支,您可以执行 array = realloc(5* sizeof *array); array[4] = set_game();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多