我真的不明白为什么我在这里出现故障。这是有史以来最简单的代码。
所以基本上,我有这个简单的节点结构
struct node {
int value;
struct node *left_child;
struct node *right_child;
};
当我像这样测试这个节点结构时:
struct node *m_node;
m_node->value = 10;
printf("%d\n", m_node->value);
这里一切正常。
现在,有了这个简单的树结构,事情就开始破裂了。
struct tree {
int size;
struct node *head;
};
我尝试像这样测试它:
struct tree *m_tree;
m_tree->head = m_node;
printf("%d\n", m_tree->head->value);
我得到了一个段错误。有任何想法吗?
您已经创建了一个悬空指针。无论是malloc()
空间,或将其分配给正在创建一个结构的地址:
struct tree *m_tree=malloc(sizeof(struct tree)); // Either this
struct tree *m_tree=&some_node; // Or this
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句