i'm kinda new with C, so this question may sound too easy.
I tried to run this simple program, but got segmentation fault:
struct list {
int data;
struct list* next;
};
void makeList(struct list* list1) {
list1 = (struct list*) malloc(sizeof(struct list));
list1->next = NULL;
}
int main(int argc, char ** argv) {
struct list * list1;
makeList(list1);
list1->data = 5;
printf("data: %d\n", list1->data);
return 0;
}
I know that the problem is list1->data = 5
in the main, but why?
The way i see it, the function makeList
gets a pointer of struct list and allocates storage for it. And that's what i did, didn't i? i passed list1
by pointer. Why is it acting like list1 is not initiallized or something? Why doesn't it work?