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