C ubuntu中的分段错误
请告诉我为什么分割错误在我的程序中没有错误。 我也试着调试它,但它从来没有进入for语句中C ubuntu中的分段错误,c,ubuntu,segmentation-fault,C,Ubuntu,Segmentation Fault,请告诉我为什么分割错误在我的程序中没有错误。 我也试着调试它,但它从来没有进入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 no
#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;
}
}
#包括
#包括
结构节点
{
int数据;
结构节点*链接;
}*启动;
main()
{
int i,n,m;
start=NULL;
printf(“输入所需的节点数”);
scanf(“%d”和“&n”);
对于(i=0;idata=data;
temp->link=NULL;
if(start==NULL)
启动=温度;
其他的
{
而(q->link!=NULL)q=q->link;
q->link=temp;
}
}
您在使用前忘记初始化q
:
q = start;
while(q->link!=NULL)
1.您尚未在
create_list()
中初始化q
并使用它-
while(q->link!=NULL)
在此循环之前初始化q=start;
2.还释放功能中为temp
分配的内存
3.main()
应该是int main(void)
并且create\u list
的类型是什么?在函数create\u list
中的main之前声明其原型本地指针q
未初始化
struct node *q,*temp;
^^^
但是,它是在循环中访问的
while(q->link!=NULL)
我想你的意思是
else
{
q = start;
while ( q->link != NULL ) q = q->link;
q->link = temp;
}
考虑到函数应在使用前声明。例如,将其声明放在main之前。其返回类型应为void
,并明确指定。同时,函数main应具有返回类型int
比如说
void create_list( int data );
int main( void )
{
//...
在退出程序之前,最好释放所有动态分配的内存
另外,标题
不是标准的C标题。您应该改用
。在上面的所有建议之后,如果您尝试显示一些清晰编码的工作,并遵守最低标准,将来会很清楚
以下是您应该如何查看代码的方法:
#include<stdio.h>
#include<stdlib.h> /* You need stdlib not malloc */
void create_list(int data); /* If you don't declare your function the compiler doesn't know nothing about create_list */
struct node{
int data;
struct node* link;
}*start;
int main(void){ /* Here return type of main is int and if no arg needed should be used void */
int i,n,m;
start=NULL;
printf("enter the number of nodes you want");
if((scanf("%d",&n)) != EOF) /* always check scanf's return */
for(i=0;i<n;i++){
printf("enter the element you want to insert");
if((scanf("%d",&m)) != EOF) /* here the same: always check scanf's return */
create_list(m);
}
return 0; /* return of main should be 0 or one of the following: EXIT_SUCCESS or EXIT_FAILURE, but 0 will be ok because it is standard*/
}
void create_list(int data){ /* here should be explicit what kind of function is */
struct node *q,*temp;
temp=(struct node *)malloc(sizeof(struct node)); /* if you allocate memory dynamically ..... */
temp->data=data;
temp->link=NULL;
if(start==NULL){
start=temp;
}else{
q = start;
while(q->link!=NULL){
q=q->link;
q->link=temp;
}
}
free(temp); /* .....then always free it */
}
#包括
#包括/*您需要的是stdlib而不是malloc*/
void create_list(int data);/*如果不声明函数,编译器对create_list一无所知*/
结构节点{
int数据;
结构节点*链接;
}*开始;
int main(void){/*此处main的返回类型为int,如果不需要参数,则应使用void*/
int i,n,m;
start=NULL;
printf(“输入所需的节点数”);
如果((scanf(“%d”,&n))!=EOF)/*始终检查scanf的返回*/
对于(i=0;这主要是一个错误的指针在create_list
中,q
在使用时是未初始化的。您是否尝试过使用调试器至少找出哪一行崩溃了?您能指出它吗?@SaeidYazdaniI怀疑第13行崩溃,这是scanf
调用,这没有什么问题。另外,请学习如何格式化和缩进您r代码,或多或少是不可读的。