I'm implementing a hash table that is supposed to store pointers to linked list of nodes containing words and their translations to a specific language.
To do so, I read from a text file the words, load them in the nodes, hash them and insert in the table. At the end of the init function, I can see the table initialised properly through ddd. However, when calling the next function in the main, and passing the table, it actually sends nothing but a pointer to an empty node instead of the table. Where did it go? Has its address changed? Is a new pointer created when passed to a function?
//main.c
//Cellule definition
typedef struct cellule{
char * mot;
char * trad;
struct cellule * suiv;
}cellule_t;
//Init and Search
cellule_t * rechercheTable(cellule_t ** tab, const char * mot){
int ind=hash_string(mot);
cellule_t * cour = tab[ind];
bool trouve=false;
while(cour!=NULL && trouve==false){
if(cour->mot==mot){
trouve=true;
}
else cour=cour->suiv;
}
return cour;
}
void initTable(cellule_t ** t){
FILE * fichier;
cellule_t *cour;
char ligne[60];
char * sep;
int i,ind;
for(i=0;i<HASH_MAX;i++){
t[i]=NULL;
}
fichier = fopen("anglais.txt","r");
if(fichier)
{
fscanf(fichier,"%s",ligne);
while(!feof(fichier))
{
cour=(cellule_t *)malloc(sizeof(cellule_t));
sep=strtok(ligne,";");
cour->mot=(char *)malloc(sizeof(char)*(((int)strlen(sep))+1));
if(sep!=NULL)
strcpy(cour->mot,sep);
ind=hash_string(sep);
sep=strtok(NULL,"\n");
if(sep!=NULL){
cour->trad=(char *)malloc(sizeof(char)*(((int)strlen(sep))+1));
strcpy(cour->trad,sep);
}
cour->suiv=t[ind];
t[ind]=cour;
fscanf(fichier,"%s",ligne);
}
fclose(fichier);
}
}
int main(){
cellule_t * tableMajeure[HASH_MAX];
cellule_t * cour;
initTable(tableMajeure);
cour = rechercheTable(tableMajeure,"hello");
printf("Resultat de la recherche de hello : %s \n",cour->mot);
return 0;
}
Tl;dr : why does tableMajeure comes out fine from Init, but is passed empty to Recherche? Thank you for your help