El siguiente es mi código actual. Mi profesor nos dijo que usáramos un puntero doble para crear una serie de punteros
struct dict { struct word **tbl; int (*hash_fcn)(struct word*); void (*add_fcn)(struct word*); void (*remove_fcn)(struct word*); void (*toString_fcn)(struct word*); }; struct word { char *s; struct word *next; };
struct dict * hashtbl;
Parte de la función principal.
hashtbl=malloc(sizeof(struct dict)); hashtbl->tbl=malloc(sizeof(struct word)*256); int i; for(i=0;itbl[i]=NULL; }
¿Es esta la forma correcta de implementar este tipo de matriz de doble puntero?
y esta usando
hashtbl->tbl[i] = .....
¿La forma correcta de acceder a ese espacio?
hashtbl->tbl=malloc(sizeof(struct word)*256);
en realidad debería ser
hashtbl->tbl=malloc(sizeof(struct word *)*256);
ya que hashtbl->tbl
es una matriz de struct word *
Para inicializar la struct word **tbl
:
hashtbl->tbl = malloc(sizeof(struct word *)*256); if (hashtbl->tbl == NULL) { printf("Error malloc"); exit(1); } for (int i = 0; i < 256; i++) { hashtbl->tbl[i] = malloc(sizeof(struct word)); if (hashtbl->tbl[i] == NULL) { printf("Error malloc"); exit(1); }
Para desasignar:
for (int i = 0; i < 256; i++) { free(hashtbl->tbl[i]); } free(hashtbl->tbl); hashtbl->tbl = NULL;