The idea is to have a singly link list of nodes and each node has a local table of data represented by another link list. I know where the seg fault is coming from, but I don't know why. Rather than paste my whole program, I typed up this stub that shows what Im trying to do. If there are typos, please ignore as the program compiles. Can anyone shed some insight into what I may be doing wrong?
Code:
typedef struct table {
char *name;
struct table *next;
} table_rec, *table_ptr;
typedef struct list {
char *listname;
struct list *next;
table_ptr table;
} list_rec, *list_ptr;
list_ptr new_list(char *name);
table_ptr new_table(char *name);
list_ptr LIST;
void main() {
list_ptr a = new_list("first");
a->next = LIST;
LIST = a; //list is a->null
list_ptr b = new_list("second");
b->next = LIST;
LIST = b; //list is b->a
//now create local data for second node
table_ptr ta = new_table("foo"); //this is fine
ta->next = LIST->table; //this causes seg fault
LIST->table = ta; //this causes seg fault
}
list_ptr new_list(char *name) {
//create node
list_ptr temp = (list_ptr)malloc(sizeof(list_rec));
temp->name = (char*)strdup(name);
temp->next = NULL;
temp->table = NULL;
return temp;
}
table_ptr new_table(char *name) {
//create node
table_ptr temp = (table_ptr)malloc(sizeof(table_rec));
temp->name = (char*)strdup(name);
temp->next = NULL;
return temp;
}