【发布时间】:2021-12-05 12:07:03
【问题描述】:
我正在迭代一个树数据结构,它有一个指向其根的指针,如下所示-
struct node *root;
当我必须将此根的引用作为参数传递给函数时......我必须像传递它一样-
calcHeight(&root);
-
-
-
//somewhere
int calcHeight(struct node **root) // function defination is this
我的问题是 - 为什么我们需要将“root”指针作为 &root 传递?我们不能像传递根一样--
struct node *root;
calcHeight(root);
int calcHeight(struct node *root);
// 编辑
void someFunct(int *arr){
printf("arr2 inside someFunct is %d\n",arr[2]);
arr[2]=30;
}
int main()
{
int *arr=(int*)calloc(10,sizeof(int));
printf("arr[2] is %d\n",arr[2]);
someFunct(arr);
printf("arr[2] finally is %d\n",arr[2]);
return 0;
}
在这种情况下,即使我没有传递 arr 的地址,主函数中的 arr 也会被修改。
我得到这样一个事实,对于结构和单值变量,我们必须传递像someFunct(&var) 这样的地址,但这对于数组来说不是必需的吗?对于数组我们写 someFunct(arr)
但我不明白这背后的原因?
【问题讨论】:
标签: c pointers tree double-pointer