代码之家  ›  专栏  ›  技术社区  ›  Cris

如何在C中填充并显示此空***矩阵?

  •  0
  • Cris  · 技术社区  · 1 年前

    上下文:我需要使用void***创建一个带有2D数组的棋盘游戏,在每个插槽中,我必须先放一个Struct“Tierra”,然后在后面的回合中,我可以放一个Struct“Bomba”

    2D数组是一个全局变量,我有一些东西

    下面是结构体Tierra:

    typedef struct Tierra{
        int vida; // random health (1-3)
        int es_tesoro; // 1 or 0 (5% chance of having a treasure)
    } Tierra;
    

    这是创建数组的函数,此函数只能接收维度作为参数,并且必须为void():

    void*** tablero;
    int dimension;
    const int chance[5]={1,2,3,4,5};
    
    void IniciarTablero(int n){
        tablero = malloc(n*sizeof(void**));
        if(tablero == NULL){
            printf("%s", "error con la memoria \n");
            exit(1);
        }   
        for( int i=0 ; i<n ; i++){
            tablero[i] = malloc(n*sizeof(void*));
            if (tablero[i] == NULL){
                printf("%s", "error con la memoria \n");
                exit(1);
            }
            for( int j=0 ; j<n ; j++){
                Tierra* tierra;
                tierra->vida = (rand() % 3) + 1;
                int probabilidad = (rand() % 100);
                int contenido = 0;
                for (int c=0 ; c<5 ; c++){
                    if(probabilidad==chance[c]){
                        contenido = 1;
                    }
                }
                tierra->es_tesoro = contenido;
                tablero[i][j] = tierra;
            }
        }
    }
    
    

    下面是我显示它的函数:

    void MostrarTablero(){
        for (int i=0 ; i<dimension ; i++){
            for (int j=0 ; j<dimension ; j++){
                Tierra* imprimir = (Tierra*)tablero[i][j];
                printf("%d | ",imprimir->vida);
            }
            printf("\n");
        }
        return;
    }
    

    每当我尝试在main中调用IniciarTablero()和MostrarTablero时,它都会给我分段错误,而且它甚至不会进入MostrarTable()。我认为问题可能在于我如何给tablero[I][j]赋值,但我是c语言的新手,还不知道如何使用指针。

    提前感谢您,希望您度过愉快的一天/一晚<3

    1 回复  |  直到 1 年前
        1
  •  0
  •   tshiono    1 年前

    正如@pridy所评论的,你不应该使用 void * 作为指针类型 在这种情况下。此外,你 为每个对象分配内存 实体 struct Tierra .
    请你试试:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    typedef struct Tierra {
        int vida; // random health (1-3)
        int es_tesoro; // 1 or 0 (5% chance of having a treasure)
    } Tierra;
    
    Tierra ***tablero;
    int dimension;
    const int chance[5] = {1, 2, 3, 4, 5};
    
    void IniciarTablero(int n)
    {
        tablero = malloc(n * sizeof(Tierra ***));
        for (int i = 0; i < n; i++) {
            tablero[i] = malloc(n * sizeof(Tierra **));
            for (int j = 0; j < n; j++) {
                Tierra *tierra = malloc(sizeof(Tierra));    // missing in the original code
                tierra->vida = (rand() % 3) + 1;
                int probabilidad = (rand() % 100);
                int contenido = 0;
                for (int c = 0; c < 5; c++) {
                    if (probabilidad == chance[c]) {
                        contenido = 1;
                    }
                }
                tierra->es_tesoro = contenido;
                tablero[i][j] = tierra;
            }
        }
    }
    
    void MostrarTablero()
    {
        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j < dimension; j++) {
                Tierra *imprimir = tablero[i][j];
                printf("%d | ", imprimir->vida);
            }
            printf("\n");
        }
    }
    
    int main()
    {
        dimension = 4;              // just for example
        IniciarTablero(dimension);
        MostrarTablero();
    
        // don't forget to free() memory after use
    
        return 0;
    }
    

    请注意,检查返回值时省略了它 malloc() 只是 为了简单起见,专注于要点。

    Btw如果值 dimension 是固定的(不是动态分配的), 最好使用简单的二维阵列 结构体Tierra .