Thread: Passing a linked list to a function

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Passing a linked list to a function

    I am trying to pass a pointer to a linked list to a function. I want the function to modify the linked list.
    In the function, If I print out a member of the structure,it works,but if I try to print in main the program breaks,which leads me to think it has something to do with bad pointers.
    Here's the code:

    Code:
    typedef struct 
    {
        int total;
        int paid;
        Item *items_ordered; 
    
    }Meal;


    Code:
    typedef struct group Group,*Pgroup;
    
    struct group
    {
        int n_elements;
        int served;
        Meal *request_meal;
        Pgroup next;
    
    };


    Code:
    void main()
    {
        ...
        Pgroup list_group=NULL;
        int n_groups;
        Table *table=NULL;
        Group *group=NULL;
        int n_tables=12;
    
                                n_groups=register_group(&group,&table,n_tables,n_groups,&list_group);
                                printf("%d\n",list_group->n_elements);//breaks
                                ...
    
        free(group);
     
    }


    Code:
    int register_group(Group **group,Table **table,int capacity,int n_groups,Pgroup *list)
    {
        int i,num_p;
    
        n_groups++;
        *group=(Group *) realloc(*group,n_groups*sizeof(Group));
    
        printf("Type the number of epople in the group:\n");
        scanf("%d",&num_p);
        
    
        for(i=0;i<capacity;i++)
        {
            printf("%d %d\n",(*table)[i].free,(*table)[i].capacity);
            if((*table)[i].livre && (*table)[i].capacity==num_p)
            {
                
                (*table)[i].suc_groups=group[n_groups-1];
                (*table)[i].free=FALSE;
                (*group)[n_groups-1].n_elements=num_p;
                (*group)[n_grups-1].served=FALSE;
                (*group)[n_groups-1].next=NULL;
                (*group)[n_groups-1].request_meal=NULL;
                return n_groups;
            }
            
        }
            printf("No tables available for that number of people!\n");
            printf("Adding to the queue...\n");
            add_group_list(list,num_p);
                    
            
        return n_groups;
    }


    Code:
    void add_group_list(Pgroup *ptr_list,int n_people)
    {
        Pgroupo new,aux;
    
        new=(Group*)malloc(sizeof(Group));
    
        if(new == NULL)
        {
            printf("Error alocating memory\n");
            exit(1);
        }
    
        if((*ptr_list)==NULL)
        {
            new->n_elements=n_people;
            new->served=FALSE;
            new->request_meal=NULL;
            new->nex=*(ptr_list);
            (*ptr_list)=new;
        }
        else
        {
            aux=(*ptr_list);
            while(aux->next!=NULL)
                aux=aux->next;
            aux->next=new;
        }
      
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Q. If you went to the trouble of making a typedef for your pointer, why aren't you using it here?
    Code:
    int register_group(Group **group,Table **table,int capacity,int n_groups,Pgroup *list)
    A. Because they SUCK! ... That's my answer, what's yours?

    If you aren't doing any kind of sorting, you don't need most of this code:
    Code:
    void add_group_list(Pgroup *ptr_list,int n_people)
    {
        Pgroupo new,aux;
    
        new=(Group*)malloc(sizeof(Group));
    
        if(new == NULL)
        {
            printf("Error alocating memory\n");
            exit(1);
        }
    
        if((*ptr_list)==NULL)
        {
            new->n_elements=n_people;
            new->served=FALSE;
            new->request_meal=NULL;
            new->nex=*(ptr_list);
            (*ptr_list)=new;
        }
        else
        {
            aux=(*ptr_list);
            while(aux->next!=NULL)
                aux=aux->next;
            aux->next=new;
        }
      
    
    }
    Just do this:
    Code:
    void add_group_list(Pgroup *ptr_list,int n_people)
    {
        if( ptr_list )
        {
            Pgroupo new;
            new=(Group*)malloc(sizeof(Group));
    
            if(new == NULL)
            {
                printf("Error alocating memory\n");
                exit(1);
            }
    
            new->n_elements=n_people;
            new->served=FALSE;
            new->request_meal=NULL;
            new->nex=*(ptr_list);
            (*ptr_list)=new;
        }
    }
    Just prepend. There's not really any reason to append over prepending. Unless of course you do actually have some reason.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, void main() is bad, mmmmkay? Read the FAQ.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The problem is you are not initializing 'new' members.

    Code:
        
         if((*ptr_list)==NULL)
        {
            new->n_elements=n_people;
            new->served=FALSE;
            new->request_meal=NULL;
            new->nex=*(ptr_list);
            (*ptr_list)=new;
        }
        else
        {
            aux=(*ptr_list);
            while(aux->next!=NULL)
                aux=aux->next;
            aux->next=new;                // new members are not initialized properly 
        }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by quzah View Post
    Just do this:
    Code:
    void add_group_list(Pgroup *ptr_list,int n_people)
    {
        if( ptr_list )
        {
            Pgroupo new;
            new=(Group*)malloc(sizeof(Group));
    
            if(new == NULL)
            {
                printf("Error alocating memory\n");
                exit(1);
            }
    
            new->n_elements=n_people;
            new->served=FALSE;
            new->request_meal=NULL;
            new->nex=*(ptr_list);
            (*ptr_list)=new;
        }
    }
    Oooh, can I be picky today...?
    No need to cast malloc in C and this can't compile as C++ because there is a variable called 'new'.

    Bazinga!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Before standardization, malloc() return char * and char pointer is used as generic pointer. That's why it's necessary to cast at that time. duh!
    http://c-faq.com/malloc/sd3.html
    Last edited by Bayint Naung; 06-09-2010 at 02:00 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to cast, you need a compiler that isn't 20 years old.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM