首页 新闻 会员 周边

单链表的创建:SIGSEGV segmentation fault 如何解决

0
[待解决问题]

用*Init_LNode()函数创建新单链表时,(r->next=p;  )这行出现SIGSEGV segmentation fault 问题。并不觉得哪里非法占用内存啊

struct LNode *Init_LNode()
{
struct LNode *head=NULL,*r=NULL,*p=NULL;
int data;


r=head;

printf("Input 数组数:");
scanf("%d",&data);
while(data!=0){
p=(struct LNode*)malloc(sizeof(struct LNode));
if(!p)exit(0);
p->data=data;
r->next=p;                       <-SIGSEGV segmentation fault 
r=p;
scanf("%d",&data);
}
r->next=NULL;
return head;
}

c
thethy的主页 thethy | 菜鸟二级 | 园豆:202
提问于:2016-10-16 17:24
< >
分享
所有回答(1)
-1

r=head复制的是 null,无法r->next

2012 | 园豆:21230 (高人七级) | 2016-10-16 20:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册