【发布时间】:2013-12-22 15:38:51
【问题描述】:
这是一个简单的程序,它从用户那里获取 5 个元素并打印出来。但它在第 30 行显示分段错误。请帮助。这是我的代码。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node * next;
};
main()
{
int i;
struct node *p,*temp,*r;
p=NULL;
temp=p;
temp=malloc(sizeof(struct node));
scanf("%d",&(temp->num));
temp->next=NULL;
for(i=0;i<4;i++)
{
while(temp->next!=NULL)
temp=temp->next;
r=malloc(sizeof(struct node));
scanf("%d",&(r->num));
r->next=NULL;
temp->next=r;
}
temp=p;
for(i=0;i<5;i++)
{
printf("%d\n",temp->num);
temp=temp->next;
}
}
【问题讨论】:
标签: c linked-list segmentation-fault