传递给函数时,为什么我的主表会重置?

时间:2019-06-06 20:18:34

标签: c hash

我正在实现一个哈希表,该哈希表应该存储指向包含单词及其对特定语言的翻译的节点的链表的指针。

为此,我从文本文件中读取单词,将其加载到节点中,对其进行散列并插入表中。在init函数的结尾,我可以看到通过ddd正确初始化了该表。但是,在主体中调用下一个函数并传递表时,它实际上只发送指向空节点而不是表的指针。去哪了地址更改了吗?传递给函数时会创建新的指针吗?

//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:为什么tableMajeure从Init出来很好,却被空传递给Recherche? 谢谢您的帮助

0 个答案:

没有答案