我正在尝试创建程序来构建链接列表,但它在创建第二个错误时给了我一个分段错误。
[root@vm c_prog]# vi link1.c
#include <stdio.h>
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main () {
int d;
struct node *root;
struct node *current;
root = malloc(sizeof(struct node));
current = root;
printf ("Location of root is %p \n", root);
d = 1;
while (d>0){
printf ("Enter the value of X: ");
scanf ("%d", ¤t->x);
printf ("value of x is stored\n");
printf ("Location of current is %p \n", current);
printf ("Value of X in 1st node: %d\n", current->x);
current = current->next;
printf ("Enter zero to terminate the loop: ");
scanf ("%d",&d);
}
}
[root@vm c_prog]# ./a.out
Location of root is 0xc34b010
Enter the value of X: 3
value of x is stored
Location of current is 0xc34b010
Value of X in 1st node: 3
Enter zero to terminate the loop: 5
Enter the value of X: 5
Segmentation fault
[root@vm c_prog]#