Thread: Segfault with Linked List Program

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Boston
    Posts
    12

    Segfault with Linked List Program

    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 the
    Code:
    printf("%s\n", temp_word->word);
    line 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.

    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);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Location
    Boston
    Posts
    12
    Found it:

    Code:
    temp_word = temp_conc->next_word;
    //should be:
    temp_word = temp_conc->next_word->next_word;
    Since the first value is never actually initialized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM