简体   繁体   中英

Why doesn't double pointer modify the table?

I have a question.

This is my structure :

typedef struct cell{

    char *mot;
    char *traduction;
    struct cell *suivant;


}cellule_t;

This is the main function where I call the function remplissage_hachage

int main()
{
    char *buffer = "hello";    
    cellule_t **tabMajeur;
    tabMajeur = malloc(HASH_MAX * sizeof(cellule_t *));
    memset(tabMajeur,0 ,HASH_MAX);
    FILE * file = fopen("fichier.txt","r");
    remplissage_hachage(tabMajeur,"fichier.txt");

}

The function remplissage_hachage takes tabMajeur as an argument:

void remplissage_hachage (cellule_t **tabMajeur,char *nomFichier)
{
    char  string1[20];
    cellule_t *copy;
    unsigned int indice;
    int boolean = 0;
    char *string2, *string3;
    cellule_t *c;


    FILE *fichier= fopen(nomFichier,"r");

    while(fgets(string1,100,fichier) != NULL)
    {
        string2 = strtok(string1,";");
        string3 = strtok(NULL,";"); 
        printf("string2 %s\n",string2);
        printf("string3 %s\n",string3);

        int indice = recherche(tabMajeur,string2,&boolean,c);

        if(boolean != 1) 
        {
            copy = tabMajeur[indice];
            tabMajeur[indice] = creationCellule(string2,string3);
            tabMajeur[indice]->suivant = copy;    
        }


    }

}

The problem is that after calling remplissage_hachage in main, tabMajeur is not modified even though I have passed it as a pointer. Can you explain to me why please?

have you checked that boolean!=1 after the call to recherche()? also you're unnecessarily opening the file twice, once in main() and once in remplissage_hachage().

if 'boolean' has the correct value then the other possible failure point is the while condition which will fail if the file is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM