c - 将指针分配给结构 C 时出现分段错误
问题描述
我的代码在通过指针传递给函数的父节点下方的树结构中创建了两个新节点。将父节点分配为新节点的父节点可以正常工作,但是程序在尝试将新节点分配为父节点的左右子节点时会抛出 seg 错误
//Create new nodes
node* new1 = (node*)malloc(sizeof(node));
new1->parent = par;
node* new2 = (node*)malloc(sizeof(node));
new2->parent = par;
par->left = new1;
par->right = new2;
这是供参考的结构
typedef struct nd {
int data;
struct nd* parent;
struct nd* left;
struct nd* right;
}node;
解决方案
已解决:我没有考虑尚未分配根的情况。(IE传递的指针为NULL)
推荐阅读
- linux - 如何使用并行命令调整多个 png 文件的大小并保留源文件中的部分名称?
- python - 如何使用文件夹中的 pandas 读取任何 excel 文件?
- postman - 在 Postman 中创建 AES 加密的标头值
- reactjs - React Material UI 响应式 Appbar
- google-play - 卡在处理更新一个多月
- python - Zillow API having issues with running basic commands
- .net - 如何获取资源文件的路径?
- node.js - 如何让 Typescript 转译我的 Sequelize 模型?
- linux - VS 代码:NPM 在命令行中运行良好,但“NPM 脚本”窗格获取“/bin/sh: 1: npm: not found”
- javascript - 按值而不是键对 Laravel pluck() 进行排序?