2

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

melpomene
  • 84,125
  • 8
  • 85
  • 148
TheSub63
  • 31
  • 4
  • Please provide a [mcve]. – melpomene Jun 06 '19 at 21:00
  • 1
    This code is very incomplete. Undefined symbols include: `hash_string`, `bool`, `false`, `NULL`, `FILE`, `HASH_MAX`. – melpomene Jun 06 '19 at 21:01
  • 1
    The problem is in the line `if(cour->mot==mot)`. You can't compare strings with `==`. Use `strcmp` to compare strings. – user3386109 Jun 06 '19 at 21:08
  • While you're at it, don't cast `malloc()`, and don't cast `strlen()` to `int`. – melpomene Jun 06 '19 at 21:09
  • 1
    @user3386109 deal! – Gerardo Zinno Jun 06 '19 at 21:14
  • BTW: when debugging code, try not to make any assumptions. You assumed that the table was empty in Recherche. But that was not true. You could have proven that the table was not empty by printing the table, or looking at the table with a debugger. – user3386109 Jun 06 '19 at 21:17
  • Also see [why is `while(!feof(fp))` always wrong?](https://stackoverflow.com/questions/5431941). The `while` loop should be `while (fscanf(fichier,"%s",ligne) == 1)`. That way you don't need the redundant `fscanf`s before the loop, and at the end of the loop. – user3386109 Jun 06 '19 at 21:23
  • @melpomene sorry I didn't wan to overload the post with library includes and basic functions such as hash_string... Why shouldn't I cast ? Is it automatic? malloc returns a void * so I thought the cast was necessary, as for strlen, I think it cannot be compared directly to an int if not casted, unless I can access some integer in the returned struct – TheSub63 Jun 07 '19 at 06:42
  • @user3386109 well actually it is through the debugger that I saw the table was empty, while it shouldn't be, that is my issue here... I was thinking about ditching fscanf for fgets to fix an issue with some trads such as "au revoir" for "bye". – TheSub63 Jun 07 '19 at 06:42
  • 1
    If you do use `fgets` be sure to [remove the newline that fgets puts in the buffer](https://stackoverflow.com/questions/2693776). – user3386109 Jun 07 '19 at 06:54

0 Answers0