加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在结构动态初始化中C(不)结构数组的结构麻烦malloc

发布时间:2020-12-16 07:06:54 所属栏目:百科 来源:网络整理
导读:我在初始化(动态)数组中的结构部分时遇到了一些小麻烦.这是我到目前为止我使用子例程来创建结构 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
我在初始化(动态)数组中的结构部分时遇到了一些小麻烦.这是我到目前为止我使用子例程来创建结构

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;

}

这是我的结构:

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;

(对不起,语言)

我现在得到的是,不是数组中结构的一切都很好.但阵列中的所有内容都是未声明的.

解决方法

这应该做的伎俩:

#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));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读