为什么我的 link 列表中存在分段错误?
Why do I have a segmentation fault in my link list?
在我输入第一个数字后,它显示分段错误,我不知道为什么。我的目标是制作一个 link 列表,其中我的根是某个数字,并输入更大的数字,这些数字最终将被添加到 link 列表的一部分。例如,根/第一个数字将是 50。我将添加一个更大的数字 60,它将被添加到 lin 列表下方的另一个节点。该过程将重复,因为我假设输入的数字会越来越大。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int num;
struct node* left;
struct node* right;
}
node;
int main(void){
printf("Put in a number:");
int x = 0;
scanf("%i", x);
node* a = malloc(sizeof(node));
a->num = x;
node* temp = a;
int n = 1;
while(n == 1){
printf("Put in another number:");
int y = 0;
scanf("%i", y);
while(n == 1){
if (temp->num < y){
if (temp->right == NULL){
node* a = malloc(sizeof(node));
temp->right = a;
temp->right->num = y;
break;
}
else{
temp = temp->right;
}
}
printf("Want to stop? Yes(1) or No (1)?");
scanf("%i", n);
}
}
}
您的问题可能会避免在您的编译器中启用警告(-Wall
对于 GCC)。 始终 检查警告,即使看起来编译仍在进行(可以通过 -W error
避免)。
通过调用 scanf ()
你是在对你的程序说:“从 stdin
读取输入,如果它与我定义的格式说明符匹配,则将其存储到特定地址“.
所以基本上它希望您传递给它的每个格式说明符都有一个地址。
有
scanf("%i", x);
你正在传递 x
,那不是地址,所以 scanf
将 尝试 写入那里,导致 分段错误 因为它可能是一个无效地址,不属于 OS 分配给您的进程的 段 。
您想要传递 x
的地址,使用一元运算符 &
:
scanf("%i", &x);
(当您为 y
和 n
变量调用 scanf
时会重复同样的错误)
scanf调用的参数
scanf("%i", x);
scanf("%i", y);
scanf("%i", n);
不正确。您必须通过引用传递变量
scanf("%i", &x);
scanf("%i", &y);
scanf("%i", &n);
这个while循环
while(n == 1){
if (temp->num < y){
if (temp->right == NULL){
node* a = malloc(sizeof(node));
temp->right = a;
temp->right->num = y;
break;
}
else{
temp = temp->right;
}
}
printf("Want to stop? Yes(1) or No (1)?");
scanf("%i", n);
}
可以调用未定义的行为,因为对于新分配的节点
if (temp->right == NULL){
node* a = malloc(sizeof(node));
temp->right = a;
temp->right->num = y;
break;
}
您没有将其数据成员 right
设置为 NULL
。所以它具有不确定的价值。结果这个 if 语句
if (temp->right == NULL){
在这条语句之后
temp = temp->right;
将此不确定值与 NULL
进行比较。
输出的字符串似乎有错字
printf("Want to stop? Yes(1) or No (1)?");
^^^^ ^^^
在我输入第一个数字后,它显示分段错误,我不知道为什么。我的目标是制作一个 link 列表,其中我的根是某个数字,并输入更大的数字,这些数字最终将被添加到 link 列表的一部分。例如,根/第一个数字将是 50。我将添加一个更大的数字 60,它将被添加到 lin 列表下方的另一个节点。该过程将重复,因为我假设输入的数字会越来越大。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int num;
struct node* left;
struct node* right;
}
node;
int main(void){
printf("Put in a number:");
int x = 0;
scanf("%i", x);
node* a = malloc(sizeof(node));
a->num = x;
node* temp = a;
int n = 1;
while(n == 1){
printf("Put in another number:");
int y = 0;
scanf("%i", y);
while(n == 1){
if (temp->num < y){
if (temp->right == NULL){
node* a = malloc(sizeof(node));
temp->right = a;
temp->right->num = y;
break;
}
else{
temp = temp->right;
}
}
printf("Want to stop? Yes(1) or No (1)?");
scanf("%i", n);
}
}
}
您的问题可能会避免在您的编译器中启用警告(-Wall
对于 GCC)。 始终 检查警告,即使看起来编译仍在进行(可以通过 -W error
避免)。
通过调用 scanf ()
你是在对你的程序说:“从 stdin
读取输入,如果它与我定义的格式说明符匹配,则将其存储到特定地址“.
所以基本上它希望您传递给它的每个格式说明符都有一个地址。
有
scanf("%i", x);
你正在传递 x
,那不是地址,所以 scanf
将 尝试 写入那里,导致 分段错误 因为它可能是一个无效地址,不属于 OS 分配给您的进程的 段 。
您想要传递 x
的地址,使用一元运算符 &
:
scanf("%i", &x);
(当您为 y
和 n
变量调用 scanf
时会重复同样的错误)
scanf调用的参数
scanf("%i", x);
scanf("%i", y);
scanf("%i", n);
不正确。您必须通过引用传递变量
scanf("%i", &x);
scanf("%i", &y);
scanf("%i", &n);
这个while循环
while(n == 1){
if (temp->num < y){
if (temp->right == NULL){
node* a = malloc(sizeof(node));
temp->right = a;
temp->right->num = y;
break;
}
else{
temp = temp->right;
}
}
printf("Want to stop? Yes(1) or No (1)?");
scanf("%i", n);
}
可以调用未定义的行为,因为对于新分配的节点
if (temp->right == NULL){
node* a = malloc(sizeof(node));
temp->right = a;
temp->right->num = y;
break;
}
您没有将其数据成员 right
设置为 NULL
。所以它具有不确定的价值。结果这个 if 语句
if (temp->right == NULL){
在这条语句之后
temp = temp->right;
将此不确定值与 NULL
进行比较。
输出的字符串似乎有错字
printf("Want to stop? Yes(1) or No (1)?");
^^^^ ^^^