data.h
struct Sub {
int n;
struct Sub *next;
}
struct Super {
struct Sub *Sub
void (*addSub)(struct Super *self, struct Sub *subRef);
}
data.c
static void addSub(struct Super *self, struct Sub *subRef) {
struct Sub *head = self->Sub;
while(head != NULL) { // throwing segmentation fault somewhere here
head = head->next;
}
// assign subRef once we reach to the end.
}
struct Super *newSuper() {
struct Super *super = malloc(sizeof(struct Super));
super->addSub = addSub;
return super;
}
data_test.c
int main() {
struct Super *super = newSuper();
super->addSub(super, malloc(sizeof(struct Sub)));
return 0;
}
I'm relatively new to C, implemented linked list long time back but can't seem to get my head around the null problem, that's how it used to be. How can I detect the end of the list and add the new value to the end?