在linux中编写c出现段错误
1#include<stdlib.h>2#include<stdlib.h>3#include<stdio.h>4#include"readline.h"567struc...
1 #include <stdlib.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "readline.h"
5
6
7 struct part *inventory = NULL;
8 struct part *find(int number)
9 {
10 struct part *p;
11 for (p = inventory; p != NULL && p->number < number; p = p->next)
12 ;
13 if(p != NULL && p->number == number)
14 return p;
15 return NULL;
16 }
17
18 void insert(void)
19 { char ch;
20 struct part *new_node, *pre;
21 struct part *cur;
22 new_node = malloc(sizeof(struct part));
23 if (new_node == NULL)
24 {
25 printf("Datebase full!");
26 return;
27 }
28 printf("Enter part number:");
29 scanf("%d", &new_node->number);
30 ch = getchar();
31
32 for(cur = inventory, pre == NULL; cur != NULL && new_node->number > cur->number; cur = pre->next, pre = cur)
33 ;
34
35 if( cur->number = new_node->number)
36 {
37 printf("The part has exist!");
38 free (new_node);
39 return;
40 }
/*******************************************************************************************************/
一下是main函数
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "readline.h"
5
6 //the main of the function
7
8 main()
9 {
10 char code;
11
12 for(;;)
13 {
14 printf("Enter operation code:");
15 scanf("%c", &code);
16 switch(code)
17 {
18 case 'i':insert();
19 break;
20 // case 's':search();
21 break;
22 // case 'u':update();
23 break;
24 // case 'p':print();
25 break;
26 case 'q':return 0;
27 default:printf("error!");
28 }
29 printf("\n");
30 }
31 }
在执行到35行的时候出现段错误,将35到40行注释掉,段错误消失。请问错误原因是什么!
答案满意给高分。 展开
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "readline.h"
5
6
7 struct part *inventory = NULL;
8 struct part *find(int number)
9 {
10 struct part *p;
11 for (p = inventory; p != NULL && p->number < number; p = p->next)
12 ;
13 if(p != NULL && p->number == number)
14 return p;
15 return NULL;
16 }
17
18 void insert(void)
19 { char ch;
20 struct part *new_node, *pre;
21 struct part *cur;
22 new_node = malloc(sizeof(struct part));
23 if (new_node == NULL)
24 {
25 printf("Datebase full!");
26 return;
27 }
28 printf("Enter part number:");
29 scanf("%d", &new_node->number);
30 ch = getchar();
31
32 for(cur = inventory, pre == NULL; cur != NULL && new_node->number > cur->number; cur = pre->next, pre = cur)
33 ;
34
35 if( cur->number = new_node->number)
36 {
37 printf("The part has exist!");
38 free (new_node);
39 return;
40 }
/*******************************************************************************************************/
一下是main函数
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "readline.h"
5
6 //the main of the function
7
8 main()
9 {
10 char code;
11
12 for(;;)
13 {
14 printf("Enter operation code:");
15 scanf("%c", &code);
16 switch(code)
17 {
18 case 'i':insert();
19 break;
20 // case 's':search();
21 break;
22 // case 'u':update();
23 break;
24 // case 'p':print();
25 break;
26 case 'q':return 0;
27 default:printf("error!");
28 }
29 printf("\n");
30 }
31 }
在执行到35行的时候出现段错误,将35到40行注释掉,段错误消失。请问错误原因是什么!
答案满意给高分。 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询