Ceci est beaucoup de code mais je suis à la fin ici essayant de comprendre pourquoi cela ne fonctionne pas.c tableau de structures
C'est le code que je suis en cours d'exécution:
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;
};
cela fait partie de la fonction principale:
hashtbl=malloc(sizeof(struct dict));
//hashtbl->tbl=malloc(sizeof(struct word*)*256);
int i;
for(i=0;i<256;i++)
{
hashtbl->tbl[i] = malloc(sizeof(struct word));
hashtbl->tbl[i] = NULL;
//hashtbl->tbl[i]=NULL;
}
hashtbl->add_fcn=&add_word;
hashtbl->remove_fcn=&remove_word;
hashtbl->hash_fcn=&hash_function;
hashtbl->toString_fcn=&toString;
FILE *p;
p = fopen("a.txt", "r");
if(p == NULL) { printf("ERROR!\n"); return 0; }
char line[MAXBYTES];
while(fgets(line, MAXBYTES, p))
{
char *token = strtok(line, " ");
while(token != NULL)
{
struct word *words = malloc(sizeof(struct word));
words->s = token;
words->next=NULL;
trim(words);
hashtbl->add_fcn(words);
token = strtok(NULL, " ");
printf("....\n");
hashtbl->toString_fcn(NULL);
printf("....\n\n");
}
free(token);
}
Voici deux fonctions utilisées (à cordes qu'afficher dehors)
void add_word(struct word *insert)
{
if(strcmp(insert->s, "\n") == 0) {return;}
int hash = hashtbl->hash_fcn(insert);
if(hash==0) { return; }
struct word *word = hashtbl->tbl[hash];
if(word==NULL)
{
struct word *newword = malloc(sizeof(struct word));
newword->s=insert->s;
newword->next=NULL;
hashtbl->tbl[hash]=newword;
printf("() %d %s \n", hash, hashtbl->tbl[hash]->s);
}
else
{
struct word *tp = word;
while(word->next != NULL)
word=word->next;
struct word *newword = malloc(sizeof(struct word));
newword->s=insert->s;
newword->next=NULL;
word->next=newword;
hashtbl->tbl[hash]=tp;
}
}
int hash_function(struct word *string)
{
char *firstletter = string->s;
char c = *firstletter;
int ascii = (int)c;
return ascii;
}
a.txt est
cat
dog
mouse
et imprime les éléments suivants:
() 99 cat
....
99 : cat
....
() 100 dog
....
99 : dog
100 : dog
....
() 109 mouse
....
99 : mouse
100 : mouse
109 : mouse
....
il doit être imprimé sur
99:cat
100:dog
109:mouse
grâce
duplication possible de [tableau de double pointeur c] (http: // stackoverflow.com/questions/8116740/c-double-pointer-array) --- à la lumière de "ne fonctionne pas" étant la seule description du problème, je suis tenté de dire que le PO devrait simplement communiquer plus dans sa question précédente à résoudre le problème. –
Avez-vous exécuté un débogueur sur ce programme? – Simon
Indice: Consultez la documentation de 'strtok()' et assurez-vous que vous comprenez exactement à qui appartient la mémoire pointée par la valeur de retour, et comment vous utilisez actuellement cette valeur. – cbranch