我正在用 C 语言为链接列表创建这个程序,以获取用户的输入,以创建一个节点并将头指向该节点并打印节点元素的值和地址。我运行时遇到段错误,请帮我解决问题。
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void main() {
head = NULL;
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
printf("Enter the number you want in node\n");
scanf("%d",(int *)temp->data);
temp->next = NULL;
head = temp;
printf("the address of node is %p",head);
printf("the value is %d",temp->data);
}
最佳答案
//try this code
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void main() {
head = NULL;
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
printf("Enter the number you want in node\n");
scanf("%d",&temp->data);
temp->next = NULL;
head = temp;
printf("the address of node is %p",head);
printf("the value is %d",temp->data);
}
关于c - C 中的链表;分段故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663468/