1
hello
I’m doing a work of algorithms in C and stopped at one part. The program has to read from an input file and insert into an n-ary Trie tree.
The code that reads from the file is this:
int abre_arquivo(TRIE_N ** root) {
FILE* arquivo;
char* word;
arquivo = fopen("dicionario.txt", "r");
if (arquivo == NULL)
printf("Arquivo não pode ser aberto.\n");
else {
while ((fscanf(arquivo, "%s\n", word)) != EOF) {
insertTrie(&(*root), word);
}
}
fclose(arquivo);
return 0;
}
the header of the insertTrie function is:
int insertTrie(TRIE_N ** root, char * word)
I wanted to know how to call the job insertTrie within the file correctly. I used gdb and is giving segmentation failure in line
current = &(*root)->children[word[0]-97];
of insertTrie. If there is any other error in the function would like to know too.
Thank you
root
has not changed inabre_arquivo
, simply passed toinsertTrie
. (But why write&(*root)
inabre_arquivo
when you can writeroot
?) It seems to me that it would be more important to demonstrate what value has passed toabre_arquivo
, and the content ofinsertTrie
.– Dan Getz
You’re not allocating your pointer
word
, when you try to enter value or write what you have inside, you find nothing and the memory error.– Brumazzi DB