我有以下简单的代码,但它引发了分段错误。有人可以指出我做错了什么吗?在我输入第一个数字后出现分段错误。
#include <stdio.h>
#include <stdlib.h>
struct linked_list {
int val;
struct linked_list *next;
} *curr, *head;
typedef struct linked_list list;
void createLinkedList(int num);
void main() {
int i;
head = NULL;
int num = 0;
for (i = 0; i < 10; i++) {
printf("Enter a number:");
scanf("%d", num);
createLinkedList(num);
}
curr = head;
while(curr) {
printf("%d\n", curr->val);
head = curr->next;
}
}
void createLinkedList(int n) {
curr = (list *)malloc(sizeof(list));
curr->val = n;
curr->next = head;
head = curr;
}
谢谢。