【发布时间】:2016-01-14 18:02:21
【问题描述】:
在我的二叉树中创建子节点后,它给了我核心转储错误,if 条件完美地工作,但是当我尝试将 sx 子节点作为参数传递时,它给出了错误,我不知道如何修复它.
#include <stdio.h>
#include <stdlib.h>
typedef struct nodes *node;
struct nodes{
int dato;
node sx;
node dx;
};
node build(node n){
printf("Insert the value: ");
scanf("%d",&n->dato );
char s[5];
printf("build a child? ");
scanf("\n%s",s);
if(s[0]=='l')
build(n->sx);
return n;
}
int main(int argc, char const *argv[]) {
system("clear");
node root=(node)malloc(sizeof(node));
root=build(root);
printf("\n\nvalue: %d\n", root->dato);
return 0;
}
【问题讨论】:
-
n->sx未初始化。 -
这显然是错误的,
node root=(node)malloc(sizeof(node));。你已经有一个node,这就是root。那你为什么要为一个人分配空间呢?
标签: c pointers segmentation-fault typedef dynamic-memory-allocation