I am learning c and this program is for me to practice implementating single linked lists. I have run into a block.
In my program I am getting a segfault in theline of my print_conc function. I think it might be error in my logic with the linked list implementation of my word_ins function but I am not sure. I have used gdb, which showed me where the segfault is, but I can't find the error in my logic that is causing it.Code:printf("%s\n", temp_word->word);
Anyone see where I am going wrong?
Thank you,
Kyle
Code:#include <stdio.h> #include <stdlib.h> struct word { const char * word; struct word * next_word; }; struct conc { char first_ltr; struct conc * next_ltr; struct word * next_word; }; int conc_ins_ltr( struct conc **next, char letter) { struct conc * temp; struct conc * new; struct word * new_word; while ( *next != NULL) { temp = *next; if ( temp->first_ltr < letter) { next = &temp->next_ltr; } else { break; } } temp = *next; new = (struct conc *)malloc( sizeof( struct conc) ); new_word = ( struct word *)malloc( sizeof( struct word) ); if ( (new == NULL) || (new_word == NULL) ) { return(0); } new_word->next_word = NULL; new->first_ltr = letter; new->next_ltr = temp; new->next_word = new_word; *next = new; return(1); } struct conc *find_first_ltr( struct conc *conc_root, char letter) { struct conc * temp = conc_root; while ( temp != NULL ) { if (temp->first_ltr == letter ) { return( temp); } temp = temp->next_ltr; } conc_ins_ltr(&conc_root->next_ltr, letter); return( find_first_ltr(conc_root, letter) ); } int word_ins( struct word **next, char * n_word) { struct word *temp; struct word *new; temp = *next; next = &temp->next_word; temp = *next; new = ( struct word *)malloc( sizeof( struct word)); if ( new == NULL ) { return(0); } new->word = n_word; new->next_word = temp; *next = new; return(1); } void print_conc( struct conc * conc_root) { struct conc * temp_conc = conc_root; struct word * temp_word; while (temp_conc != NULL) { printf("%c\n", temp_conc->first_ltr); temp_word = temp_conc->next_word; while ( temp_word != NULL) { printf("%s\n", temp_word->word); temp_word = temp_word->next_word; } temp_conc = temp_conc->next_ltr; } } int main( int argc, char **argv) { struct conc conc_lvl1; conc_lvl1.next_ltr = NULL; conc_ins_ltr( &conc_lvl1.next_ltr, 'a' ); conc_ins_ltr( &conc_lvl1.next_ltr, 'b' ); word_ins( &(find_first_ltr(&conc_lvl1, 'a')->next_word), "apple"); print_conc(conc_lvl1.next_ltr); return(0); }



LinkBack URL
About LinkBacks


