user3084993
user3084993

Reputation: 43

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.

Upvotes: 4

Views: 138

Answers (3)

Fiddling Bits
Fiddling Bits

Reputation: 8861

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

Upvotes: 1

tonga
tonga

Reputation: 11971

You can use malloc() to allocate memory for each row. 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);
}

Upvotes: -1

Greg Hewgill
Greg Hewgill

Reputation: 993173

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.

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:

  • declare a single dimension array of size dim_ligne*dim_col of t_case_animal values
  • declare an array of row pointers of size dim_ligne that each point to another single dimensional array of dim_col values

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

t_case_animal * la_grille;

and access your values as something like 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].

Upvotes: 1

Related Questions