[英]C (not ++) a struct of struct array in struct dynamic initialization troubles malloc
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.
但阵列中的所有内容都是未声明的。
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));
The malloc()
function does not (necessarily) initialise the allocated bytes to any value in particular. malloc()
函数不一定(必然)将分配的字节初始化为任何值。 So after calling malloc()
, you should explicitly initialise the allocated data. 因此,在调用
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没有真正的多维数组,您可以:
dim_ligne*dim_col
of t_case_animal
values t_case_animal
值的大小为dim_ligne*dim_col
的单维数组 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
的声明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]
那样访问您的值。
You can use malloc()
to allocate memory for each row. 您可以使用
malloc()
为每一行分配内存。 The following code should work: 以下代码应该有效:
#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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.