storing several strings from a recursive function into a struct c -
i making predictive text interface store dictionary data structure (ive used trie), user searches partially word , completed words displayed numbers corresponding each one. have done insertion, search functions , have recursive traversal prints out completed words(with no numbers). want store them struct can use them in function user shown words corresponding numbers.
heres main.c code (for testing not go readfile enters 25 000 words!):
struct trienode* root = trierootconstructor(); struct trienode* pntr = null; trieinsert(root, "aback"); trieinsert(root, "abacus"); trieinsert(root, "abalone"); trieinsert(root, "abandon"); trieinsert(root, "abase"); trieinsert(root, "abash"); trieinsert(root, "abate"); trieinsert(root, "abater"); int x = 0; char* result = ""; char* search = "aba"; result = triesearch(root, &pntr, search, result, &x); printf("\n\n"); traversetwo(pntr, search);
pntr set node partial word ends, traversal search rest of word.
here recursive traversal , caller:
void traversetwo(struct trienode* node, char* partialword) { char arr[50]; int index = 0; int maxwordsize = 100; char wordarr[50][maxwordsize]; index = recursiveprint(node->children, arr, wordarr[50], 0, partialword, index); int = 0; for(i = 0; < index; i++) printf("%d: %s\n", i, wordarr[i]); printf("%d: continue typing", index); } int recursiveprint(struct trienode* node, char* arr, char* wordarr, int level, char* partialword, int index) { if(node != null) { arr[level] = node->symbol; index = recursiveprint(node->children, arr, wordarr, level+1, partialword, index); if(node->symbol == '\0') index = completewordandstore(partialword, arr, wordarr, index); index = recursiveprint(node->sibling, arr, wordarr, level, partialword, index); } return index; } int completewordandstore(char* partialword, char* restofword, char* wordarr, int index) { int length = strlen(partialword) + strlen(restofword); char completeword[length]; strcpy(completeword, partialword); strcat(completeword, restofword); strcpy(wordarr[index], completeword); index++; return index; }
i getting segmentation fault on strcpy(wordarr[index], completeword);
the idea (in head) once goes if statement nodes symbol '\0' store string @ value of index.
partial word part word has been searched ee.g "aba", strcat arr , store struct.
the result should produce:
0: aback 1: abacus 2: abalone 3: abandon 4: abase 5: abash 6: abate 7: abater 8: continue typing
i call destructor later on words tried , tested.
can suggest how modify can store strings??
i assume array structure if correct??
many thanks
jack
char* wordarr[50];
you're not allocating memory words. try with:
int maxwordsize = 100; char wordarr[50][maxwordsize];
Comments
Post a Comment