C (not ++)结构体动态初始化中结构体阵列的结构体问题

时间:2022-09-06 11:01:31

I have a small trouble initializing (dynamic) parts of my structures that are in an array. This is what i have so far I am using a sub-routine to create the struct

初始化(动态)数组中的结构部分有一个小问题。到目前为止,我使用子例程创建结构体

t_grille_animaux creer_grille(int dim_ligne, int dim_col) 
{
    t_grille_animaux grille;

    grille.la_grille = (t_case_animal **) malloc(sizeof(t_case_animal)*dim_ligne*dim_col);

    grille.dim_colonne = dim_col;

    grille.dim_ligne = dim_ligne;

    grille.nb_predateurs = NULL;

    grille.nb_proies = NULL;

    return grille;

}

This is my structure:

这是我的结构:

typedef struct
{
    t_case_animal ** la_grille; //2D array
    int dim_ligne;
    int dim_colonne;
    int nb_proies;
    int nb_predateurs;
} t_grille_animaux;

typedef struct
{
    t_contenu etat;
    t_animal animal;
} t_case_animal;

typedef enum {VIDE, PROIE, PREDATEUR} t_contenu;

typedef struct
{ 
    int age;           
    int jrs_gestation; 
    int energie;      
    int disponible;    
} t_animal;

(Sorry for the language)

(抱歉的语言)

What I get right now is that everything that isn't the struct in the array is fine. But everything in the array is undeclared.

我现在得到的是数组中非结构体的所有东西都没问题。但是数组中的所有内容都是未声明的。

3 个解决方案

#1


1  

This should do the trick:

这应该可以做到:

#define NUM_ROWS (10)
#define NUM_COLS (15)

grille.la_grille = malloc(NUM_ROWS * sizeof(*grille.la_grille));
for(int row = 0; row < NUM_ROWS; row++)
    grille.la_grille[row] = malloc(NUM_COLS * sizeof(**grille.la_grille));

#2


1  

The malloc() function does not (necessarily) initialise the allocated bytes to any value in particular. So after calling malloc(), you should explicitly initialise the allocated data.

malloc()函数并不(一定)将分配的字节初始化为任何特定的值。因此,在调用malloc()之后,应该显式地初始化已分配的数据。

Having said that, you have a couple of choices about how you can store your two-dimensional array. It depends on how you want to access the data. Since C does not have true multidimensional arrays, you can either:

说到这里,关于如何存储二维数组,您有几个选择。这取决于您希望如何访问数据。由于C没有真正的多维数组,所以您可以:

  • declare a single dimension array of size dim_ligne*dim_col of t_case_animal values
  • 声明大小为t_case_animal值的dim_ligne*dim_col的单个维数组
  • declare an array of row pointers of size dim_ligne that each point to another single dimensional array of dim_col values
  • 声明一个具有dim_ligne大小的行指针数组,每个指针指向另一个具有dim_col值的一维数组

For the first case, change your declaration of la_grille to:

对于第一种情况,将您的la_grille声明更改为:

t_case_animal * la_grille;

and access your values as something like la_grille[j*dim_colonne+i].

将值访问为la_grille[j*dim_colonne+i]。

For the second case, be sure to initialise your subarrays:

对于第二种情况,请确保初始化子数组:

grille.la_grille = (t_case_animal **) malloc(sizeof(t_case_animal*)*dim_ligne);
for (int i = 0; i < dim_ligne; i++) {
    grille.la_grille[i] = (t_case_animal *) malloc(sizeof(t_case_animal)*dim_col);
}

In the second case, you would access your values as something like la_grille[j][i].

在第二种情况下,您可以像访问la_grille[j][i]那样访问您的值。

#3


-1  

You can use malloc() to allocate memory for each row. The following code should work:

可以使用malloc()为每一行分配内存。下列守则应有效:

#include<stdlib.h>

typedef struct
{ 
    int age;           
    int jrs_gestation; 
    int energie;      
    int disponible;    
}t_animal;

typedef enum {VIDE, PROIE, PREDATEUR} t_contenu;

typedef struct
{
    t_contenu etat;
    t_animal animal;
} t_case_animal;

 typedef struct
{
    t_case_animal ** la_grille; //2D array
    int dim_ligne;
    int dim_colonne;
    int nb_proies;
    int nb_predateurs;
} t_grille_animaux;


t_grille_animaux creer_grille(int dim_ligne,int dim_col)
{

t_grille_animaux grille;


    grille.la_grille = (t_case_animal**) malloc(sizeof(t_case_animal*)*dim_ligne);

    for(int i=0; i<dim_ligne; i++) {
        grille.la_grille[i] = (t_case_animal*) malloc(sizeof(t_case_animal)*dim_col);
    }

grille.dim_colonne = dim_col;

grille.dim_ligne = dim_ligne;

grille.nb_predateurs = 0;

grille.nb_proies = 0;

return grille;

}

int main(int argc, char* argv[])
{
    t_grille_animaux test;
    test = creer_grille(3, 4);
}

#1


1  

This should do the trick:

这应该可以做到:

#define NUM_ROWS (10)
#define NUM_COLS (15)

grille.la_grille = malloc(NUM_ROWS * sizeof(*grille.la_grille));
for(int row = 0; row < NUM_ROWS; row++)
    grille.la_grille[row] = malloc(NUM_COLS * sizeof(**grille.la_grille));

#2


1  

The malloc() function does not (necessarily) initialise the allocated bytes to any value in particular. So after calling malloc(), you should explicitly initialise the allocated data.

malloc()函数并不(一定)将分配的字节初始化为任何特定的值。因此,在调用malloc()之后,应该显式地初始化已分配的数据。

Having said that, you have a couple of choices about how you can store your two-dimensional array. It depends on how you want to access the data. Since C does not have true multidimensional arrays, you can either:

说到这里,关于如何存储二维数组,您有几个选择。这取决于您希望如何访问数据。由于C没有真正的多维数组,所以您可以:

  • declare a single dimension array of size dim_ligne*dim_col of t_case_animal values
  • 声明大小为t_case_animal值的dim_ligne*dim_col的单个维数组
  • declare an array of row pointers of size dim_ligne that each point to another single dimensional array of dim_col values
  • 声明一个具有dim_ligne大小的行指针数组,每个指针指向另一个具有dim_col值的一维数组

For the first case, change your declaration of la_grille to:

对于第一种情况,将您的la_grille声明更改为:

t_case_animal * la_grille;

and access your values as something like la_grille[j*dim_colonne+i].

将值访问为la_grille[j*dim_colonne+i]。

For the second case, be sure to initialise your subarrays:

对于第二种情况,请确保初始化子数组:

grille.la_grille = (t_case_animal **) malloc(sizeof(t_case_animal*)*dim_ligne);
for (int i = 0; i < dim_ligne; i++) {
    grille.la_grille[i] = (t_case_animal *) malloc(sizeof(t_case_animal)*dim_col);
}

In the second case, you would access your values as something like la_grille[j][i].

在第二种情况下,您可以像访问la_grille[j][i]那样访问您的值。

#3


-1  

You can use malloc() to allocate memory for each row. The following code should work:

可以使用malloc()为每一行分配内存。下列守则应有效:

#include<stdlib.h>

typedef struct
{ 
    int age;           
    int jrs_gestation; 
    int energie;      
    int disponible;    
}t_animal;

typedef enum {VIDE, PROIE, PREDATEUR} t_contenu;

typedef struct
{
    t_contenu etat;
    t_animal animal;
} t_case_animal;

 typedef struct
{
    t_case_animal ** la_grille; //2D array
    int dim_ligne;
    int dim_colonne;
    int nb_proies;
    int nb_predateurs;
} t_grille_animaux;


t_grille_animaux creer_grille(int dim_ligne,int dim_col)
{

t_grille_animaux grille;


    grille.la_grille = (t_case_animal**) malloc(sizeof(t_case_animal*)*dim_ligne);

    for(int i=0; i<dim_ligne; i++) {
        grille.la_grille[i] = (t_case_animal*) malloc(sizeof(t_case_animal)*dim_col);
    }

grille.dim_colonne = dim_col;

grille.dim_ligne = dim_ligne;

grille.nb_predateurs = 0;

grille.nb_proies = 0;

return grille;

}

int main(int argc, char* argv[])
{
    t_grille_animaux test;
    test = creer_grille(3, 4);
}