为什么当我将非 NULL 指针传递给方法时,它们似乎会转换为 NULL?(C++)
Why do non-NULL pointers seemingly convert to NULL when I pass them to a method? (C++)
我有一个二叉树,我想打印出来。我的树结构是:
struct tree
{
Node * root;
tree() {root = NULL;}
Node * insert(Node * n, people * pp);
void print(Node * pp)
{
if(pp==NULL)
{ cout<<"Node sent to print is null"<<endl; return;}
print(pp->left);
cout<<pp->p->lname<<endl;
print(pp->right);
}
};
总的来说,我是这样称呼的:
if(tr->root == NULL) cout<<"drat";
tr -> print(tr->root);
我完全确定 tr->root 不是 NULL,考虑到我只是在打印之前遵循了一行。为什么我的打印方法坚持要传递空值?
您的print()
函数是递归的。 它将沿着树向下移动,直到找到空叶并打印该消息。然后它将打印其余部分。
我建议以下修改:
void print(Node * pp)
{
if(pp==NULL)
{ cout<<"Node sent to print is null"<<endl; return;}
if (pp->left) print(pp->left);
cout<<pp->p->lname<<endl;
if (pp->right) print(pp->right);
}
相关文章:
- 防止主数据类型C++的隐式转换
- 将以 null 结尾的字节字符串转换为原始字符串文本
- 将位数组转换为 HBITMAP 后 bmBits 的 NULL 指针
- 将 NULL 转换为长不是模棱两可吗?
- 在构造函数处将类对象强制转换为接口始终返回 NULL
- 无法将模板参数null转换为void*
- 在Linux/GCC下,将NULL指针访问转换为C++异常
- C++将 NULL 和非 NULL 值转换为指针
- 返回 NULL,但收到错误 C2440:'return':无法从'int'转换为'const &'
- QImage转换返回null
- 为什么当我将非 NULL 指针传递给方法时,它们似乎会转换为 NULL?(C++)
- 将 NULL/nullptr 转换为 std::function<>& 时失败
- C++将时间 (NULL) 转换为本地时间的函数
- Delphi - 从 dll 调用转换(读取)C++ NULL 终止数组
- C++将文本文件写入字符串并转换为double,会得到null char或0
- srand(time(null))导致编译器警告:隐式转换会丢失整数精度
- atoi中的Null将值转换为0
- 将unsigned char *buf=NULL转换为Pascal
- 可以安全地将预先存在的类型转换NULL的返回与较新的nullptr进行比较吗
- 为什么指向 NULL 字符的指针没有转换为 false