【发布时间】:2020-08-22 07:04:52
【问题描述】:
typedef struct sTree {
int key;
struct sTree* p;
struct sTree* left;
struct sTree* right;
} sTree;
typedef sTree* tree;
void treeInsert(tree* root);
int main(){
srand((unsigned)time(NULL));
tree *root = NULL;
treeInsert(&root);
return 0;
}
void treeInsert(tree* root){
......
}
我不明白为什么在调用树插入(&root 而不是 root)时必须传递 '&'。 我创建了一个表示二叉树节点的结构,并将指向树根的指针声明为“tree*”。 所以'root'是双指针。函数“treeInsert”需要一个双指针。如果我简单地传递'root',它会使用运算符'&'获取值(NULL),否则它会正确指向根。问题是: 传递 '&root' 我没有传递三重指针?有人可以解释为什么吗?
【问题讨论】:
-
参见Is it a good idea to typedef pointers? TL;DR — 答案是否定的,函数指针可能例外。
-
@JohnnyMopp 查看
void treeInsert(tree* root)指纹。 Typedef 与否,root可以用于该功能。我们不知道该功能是如何实现的。让我们避免猜测。 -
@davide 编译器是否可以将
&root传递给treeInsert?
标签: c pointers tree computer-science double-pointer