【发布时间】:2019-10-30 10:30:11
【问题描述】:
我知道我可以更改代码来修复错误,但我不明白为什么会出现分段错误。感谢所有帮助,谢谢。
typedef struct nodes{
int data;
struct nodes *next;
} node;
int main(int argc, char * argv[]){
node *head = NULL;
node *tmp = NULL;
int i;
head = malloc(sizeof(node));
tmp = head;
for(i = 0; i < 10; i++){
tmp->data = i;
tmp->next = malloc(sizeof(node));
tmp = tmp->next;
}
tmp = NULL;
for(tmp=head; tmp->next != NULL; tmp = tmp->next){
printf("%d\n", tmp->data);
}
}
这是输出:
0
1
2
3
4
5
6
7
8
9
0
Segmentation fault: 11
【问题讨论】:
标签: c list linked-list segmentation-fault nodes