【发布时间】:2015-08-16 09:14:12
【问题描述】:
请告诉我为什么我的程序中存在分段错误,没有错误。 我也尝试调试它,但它从未进入 for 语句。
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node* link;
} *start;
main()
{
int i,n,m;
start=NULL;
printf("enter the number of nodes you want");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element you want to insert");
scanf("%d",&m);
create_list(m);
}
}
create_list(int data)
{
struct node *q,*temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
while(q->link!=NULL) q=q->link;
q->link=temp;
}
}
【问题讨论】:
-
它主要是一个坏指针
-
in
create_list,q在使用时未初始化。 -
您是否尝试过使用调试器至少找出哪条线路崩溃?
-
你能指出来吗?@SaeidYazdani
-
我怀疑第 13 行崩溃,这是
scanf调用,这并没有错。另外,请学习如何格式化和缩进你的代码,它或多或少是不可读的。
标签: c ubuntu segmentation-fault