I dont understand the cause of the segmentation fault here. The code is:
struct node {
int data;
struct node* next;
};
void add(int a,struct node *lista)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node*));
p->data=a;
p->next=NULL;
while(lista->next!=NULL) <--- The segmentation fault is here.
lista=lista->next;
lista->next=p;
return lista;
}
int main(void)
{
struct node *list=NULL;
list_print(list);
list=node123();
list_print(list);
add(7, &list);
list_print(list);
return 0;
}
the add function which adds a new node to the end of the list worked perfectly like this on a friends computer and setup. I get segmentation fault. i think the problem is the lista->next
expression but I don't understand why. Any ideas?