I am trying to initialise a struct by allocating memory to it and it's pointer members using malloc:
typedef struct {
char *name;
prob_t *prob;
} name_t;
I understand that I need to allocate memory to the pointers separately once the struct is initialised:
name_t
*init_name_dict() {
name_t *name_dict;
name_dict = (name_t*)malloc(MAX_LINES*sizeof(*name_dict));
name_dict->name = (char*)malloc(MAX_LEN*sizeof(*name_dict->name));
name_dict->prob = (prob_t*)malloc(MAX_PROB*sizeof(*name_dict->prob));
return name_dict;
}
But when I do so, it allocates memory to the struct, but not to either of its member pointers (they just point to junk).
What am I doing wrong? Thanks