指针应用时的注意事项

1. 对指针进行取值之前,一定要保证指针不为空

空指针不可取其值或者继续访问

#define BinNodePosi(T) BinNode<T>*

template <typename T> 
struct BinNode{
    T data;
    BinNodePosi(T) lChild;
    BinNodePosi(T) rChild;
};

#define IsLChild(x) (x).parent->lChild == x
                    // (x).parent->lChild ⇒ 处理是有危险的
                    // 要首先保证 x.parent 不为空,
#define IsLChild(x) (!IsRoot(x) && (x).parent->lChild == x)
  • 对任何二叉树结点而言,都会存在:数据域、左指针域以及右指针域(这是结点结构体的三大必要构成),
    (为不为 NULL,就不一定了)
    只不过再进行取值(*p)或者访问(.或者->)操作时,需要首先判断是否为空;
原文地址:https://www.cnblogs.com/mtcnn/p/9423747.html