Like Tree1Likes
  • 1 Post By Subsonics

Learning about linked lists, getting "Segmentation fault (core dumped)" error

This is a discussion on Learning about linked lists, getting "Segmentation fault (core dumped)" error within the C Programming forums, part of the General Programming Boards category; Here's the code i'm working on. As far as I can tell the problem concerns passing in 'head' to 'insertEntry', ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Question Learning about linked lists, getting "Segmentation fault (core dumped)" error

    Here's the code i'm working on. As far as I can tell the problem concerns passing in 'head' to 'insertEntry', or maybe it's just passing 'head' to any function - I'm totally at a loss as to how to fix it. I haven't gotten around to filling in the 'removeEntry' function yet.

    Code:
    # include <stdio.h>
    #include <stdlib.h>
    
    
    struct entry{
        int val;
        struct entry* next;
    };
    
    
    void insertEntry(struct entry* this, struct entry* after);
    
    
    void removeEntry(struct entry* after);
    
    
    void printList(struct entry* head);
    
    
    int main(){
        
        struct entry* head;
        struct entry* temp_ptr;
        
        int i;
        int val;
        
        // initialize the beginning of the list
        head = NULL;
        
        // print the list
        printList(head);
        
        // insert 3 items
        for( i=0; i<3; i++ ){
            printf("Enter a value: ");
            scanf(" %d", &val);
            
            //allocate and initialize a new node
            temp_ptr = (struct entry*) malloc(sizeof(struct entry));
            temp_ptr->val = val;
            
            //insert the new node into the list
            insertEntry(temp_ptr, head);
        }
        
        // print the elements
        printList(head);
        
        // remove 3 items
    //    removeEntry(head);
    //    removeEntry(head);
    //    removeEntry(head);
        
        //print the list
        printList(head);
        
        
        return 0;
    }
    
    
    
    
    void insertEntry(struct entry* this, struct entry* after){
        this->next = after->next;
        after->next = this;
    }
    
    
    void removeEntry(struct entry* after){
    
    
    }
    
    
    
    
    void printList(struct entry* head){
        struct entry* curr = head->next;
        
        printf("List is: ");
        
        if(!curr){
            printf("EMPTY\n");
            return;
        }
        
        while(curr){
            printf("%d ",curr->val);
            
            curr = curr->next;
        }
        printf("/n");
        
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You start by setting an empty pointer to NULL, then you pass that to printList. The first thing printList does is to dereference head (your NULL pointer) which will not work. You are trying to print a non-existing list.
    iMalc likes this.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Hmm.. well going off your suggestion I changed

    Code:
    void printList(struct entry* head){    
    
    struct entry* curr = head->next;    
        printf("List is: ");
    to just 'struct entry* curr = head;' and that seems to work, but I don't know what I did. So... by setting 'head = NULL' that sets its address to 0? And then the printList tries to... I don't know

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Well, this doesn't exactly work. How would i go about initializing head to work with my program?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are doing the same thing in insertEntry(), you are dereferencing 'after' which is you head (which is NULL). Typically insertEntry should take the address to your head and the data you want to insert, creation of new nodes and linking should happen inside insertEntry(), not in main().

    http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
    Last edited by Subsonics; 04-10-2012 at 01:57 PM.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    OHH! I finally get it! Thank you Subsonics, that link really helped.

    I made the initialization into a malloc so that it wouldn't be an empty pointer anymore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2011, 11:57 AM
  2. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  3. how to fix "Segmentation fault (core dumped)"
    By kapil1089thekin in forum C++ Programming
    Replies: 5
    Last Post: 08-20-2010, 04:24 AM
  4. Replies: 1
    Last Post: 11-16-2007, 07:31 PM
  5. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21