Thread: Linked list crashes

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    Linked list crashes

    Hi there,

    I was wondering... this code crashes when volg==NULL at (*p)->volg

    But why does it crash? I need to check if this value is NULL
    Any idea?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct record
    {
    	int getal;
    	struct record *volg;
    }ELT;
    
    void toon_list(ELT** p)
    {
    	if(*p)
    	{
    		if((*p)->volg)
    			toon_list((*p)->volg);
    		printf("%d\n", (*p)->getal);
    
    	}
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void toon_list(ELT** p)
    There's really no reason for this to be a double pointer. You could just as well use:
    Code:
    void toon_list(ELT* p)
    {
    	if(p)
    	{
    		if(p->volg)
    			toon_list(p->volg);
    		printf("%d\n", p->getal);
    
    	}
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    yeah, right, I knew that, but thank you anyway mate.

    The assignment mentioned a pointer to pointer structure, and I was trying some stuff.

    But still, it doesn't explain the exception error?

    Any idea about that? I really need this to work.

    Thanx swoopy!

    Greetings

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >this code crashes when volg==NULL
    Are you sure it's NULL? The error sounds more like you neglected to terminate your list properly and the last volg isn't actually a null pointer. Show us a complete program that crashes.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The assignment mentioned a pointer to pointer structure

    > toon_list((*p)->volg);
    Here the compiler should give you a warning about a type mismatch, as you're passing a pointer to ELT, but the function expects a double pointer, so you might try this:
    Code:
    			toon_list(&(*p)->volg);

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    This is the code, I think I terminated it well, and I fixed that unneeded double pointer
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct record
    {
    	int getal;
    	struct record *volg;
    }ELT;
    
    void maak_list(ELT**, int*);
    ELT* koppel(ELT *kop1, ELT *kop2);
    
    void toon_list(ELT*);
    
    int main(void)
    {
    	int tab1[]={1, 2, 3, 4, 5, 0};
    	int tab2[]={6, 7, 8, 9, 0};
    
    	ELT *kop1=NULL, *kop2=NULL, *kop;
    
    	maak_list(&kop1, tab1);
    	maak_list(&kop2, tab2);
    	//kop=koppel(kop1, kop2);
    	//toon_list(&kop);
    	toon_list(kop1);
    	toon_list(kop2);
    
    	return 0;
    }
    
    void maak_list(ELT **pkop, int* tab)
    {
    	ELT *p, *vorig;
    	int i;
    
    	p=malloc(sizeof(ELT));
    	p->getal=*tab;
    	(*pkop)=p;
    	vorig=pkop;
    
    	for(i=1; tab[i]; i++)
    	{
    		p=malloc(sizeof(ELT));
    		p->getal=tab[i];
    		vorig->volg=p;
    		vorig=p;
    	}
    
    	p->volg=NULL;
    }
    
    
    void toon_list(ELT* p)
    {
    	if(p)
    	{
    		if(p->volg)
    			toon_list(p->volg);
    		printf("%d\n", p->getal);
    
    	}
    }

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    PLEAAAASE,really need some help on this one!!!! My exams are on monday! This problem happens on every exercise I make!

    When debugging I can see that kop is still a value, and kop->volg=NULL, but WHY in eaerth does it crash????? It shouldn't crash should it???

    Greetings, ANY reply would be GREATLY! appreciated, for I am getting into a state of panic since this sorta exercise WILL be my exam;
    thanx!

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Shouting for help doesn't do much good.
    It's better to listen to compiler warnings.
    works this way
    Code:
    void maak_list(ELT **pkop, int* tab)
    {
    	ELT *p, *vorig;
    	int i;
    
    	p=malloc(sizeof(ELT));
    	p->getal=*tab;
    	(*pkop)=p;
    	vorig=*pkop;
    
    	for(i=1; tab[i]; i++)
    	{
    		p=malloc(sizeof(ELT));
    		p->getal=tab[i];
    		vorig->volg=p;
    		vorig=p;
    	}
    
    	p->volg=NULL;
    }
    Kurt

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    Dude I love you

    Thank you I owe you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM