2017-07-27 3 views
1

J'essaie de faire ma propre implémentation d'un arbre trie pour soumettre une liste de mots en C en stockant les caractères dans des tableaux de caractères puis d'accéder au noeud suivant pour stocker le tableau suivant, chaque noeud contenu dans un tableau de noeuds, mais quand je le débogue, il semble que la connexion avec le prochain tableau de noeuds est perdue parce qu'il dit qu'il est nul.Pourquoi mon tableau de pointeurs a-t-il une segmentation faul en C?

C'est le struct:

typedef struct node { 
    char c[ALLCHAR]; 
    struct node *next[ALLCHAR]; 
} listword; 

listword *head; 
listword *current; 

Et ceci est la mise en œuvre:

head = malloc(sizeof(listword));  //Initialize the head 
current = head;      //Pass the pointer to current 
dict = fopen(dictionary, "r");   //This is the source of the words 

if(dict==NULL) { 
    fclose(dict); 
    return 1; 
} 
//Here I iterate char by char in the dict 
for(int c=fgetc(dict);c!=EOF;c=fgetc(dict)) 
{ 
    int myc=tolower(c)-97;   //The index equivalent to the char 
    if(c=='\n') { 
      current->c[27]='\0';  //The end of the word 
      current=head;   //Pass the start pointer to the current 
    } else { 
     current->c[myc]=c;   //Here it puts the char in its position 
     current=current->next[myc]; //Here I identify my problem, even 
            //when I already initialized next it 
            //appears as no value 
     if(!current) { 
      current=malloc(sizeof(listword)); //Here I initiliaze next 
               //if haven't initiliazed yet 
     } 
    } 
} 

S'il vous plaît aidez-moi à ce problème, merci à l'avance.

+2

maintenant est un bon moment pour apprendre à utiliser un débogueur pour déboguer votre code. Une fois que vous avez fait cela, et recueilli quelques détails pertinents, s'il vous plaît modifier votre question, et publiez ce que vous avez trouvé – OldProgrammer

+0

êtes-vous sûr que '0 <= myc yano

+1

Le fichier contient-il des retours à la ligne? Si c'est le cas, vous utiliserez ''\ n' - 91' comme index de tableau - kaboom! Au moins jusqu'à ce qu'il fonctionne, je suggère de vérifier l'index de tableau. –

Répondre

0

Essayez ceci,

#include <stdio.h>  
#include <stdlib.h> 

typedef struct node { 
    char data[1024]; 
    struct node *next; 
} listword; 

listword *head = NULL; 


// add string to listword 
void add(char *value) { 
    listword *tmp; 

    tmp = (listword*) malloc(sizeof(listword)); 
    if (tmp) { 
     memcpy(tmp, value, sizeof(tmp)); 

     tmp->next = head; 
     head = tmp; 
    } 
}  

// show data of listword 
void show() { 
    listword *tmp; 

    tmp = head; 
    while (tmp) { 
     printf("%s\n", tmp->data);   

     tmp = tmp->next; 
    } 
} 

int main() { 
    FILE * file; 
    char * line = NULL; 
    size_t len = 0; 
    ssize_t buffer; 

    // open 
    file = fopen("dictionary", "r"); 
    if (file == NULL) { 
     exit(1); 
    } 

    // read line of lines of file and add to listword 
    while ((buffer= getline(&line, &len, file)) != -1) { 
     add (line); 
    } 

    // close and free file 
    fclose(file); 
    if (line) { 
     free(line); 
    } 

    // show data 
    show(); 

    return (0); 
}