为什么在c的结构中使用两个指针
我在c中读到了关于树的内容:为什么在c的结构中使用两个指针,c,C,我在c中读到了关于树的内容: struct node { int key_value; struct node *left; struct node *right; }; /* insert a value to tree */ insert(int key, struct node **leaf) { if( *leaf == 0 ) { *leaf = (struct node*) malloc( sizeof( struct node ) );
struct node
{
int key_value;
struct node *left;
struct node *right;
};
/* insert a value to tree */
insert(int key, struct node **leaf)
{
if( *leaf == 0 )
{
*leaf = (struct node*) malloc( sizeof( struct node ) );
(*leaf)->key_value = key;
/* initialize the children to null */
(*leaf)->left = 0;
(*leaf)->right = 0;
}
else if(key < (*leaf)->key_value)
{
insert( key, &(*leaf)->left );
}
else if(key > (*leaf)->key_value)
{
insert( key, &(*leaf)->right );
}
}
struct节点
{
int key_值;
结构节点*左;
结构节点*右;
};
/*在树中插入一个值*/
插入(整型键,结构节点**叶)
{
如果(*leaf==0)
{
*叶=(结构节点*)malloc(sizeof(结构节点));
(*leaf)->key_值=key;
/*将子项初始化为null*/
(*叶)->左=0;
(*叶)->右=0;
}
否则如果(键<(*叶)->键值)
{
插入(键和(*叶)->左侧);
}
else if(键>(*叶)->键值)
{
插入(键和(*叶)->右侧);
}
}
这里我不明白:insert(int键,结构节点**leaf)为什么两个指针**leaf,*leaf行吗?我不知道什么时候该用两个指针。请帮忙,谢谢 C只有传递值。如果指向节点结构的指针用作参数,则调用方函数中不会看到对传递的指针的任何修改。在这种情况下,必须从函数
insert
返回指针。指针指针在这里用于更新传递给函数insert
的指针,当您希望通过某个函数更改main()
中定义的变量值时。想想你是做什么的。发送该变量的地址,然后使用该地址更改该变量的内容
现在,在一个例子中,变量是int
类型,因此发送该变量的地址意味着在函数中,您必须在int*
类型的变量中接收它
void test(int* var) {
*var++;
}
int main() {
int integer = 1;
test(&integer);
printf("%d", integer);
return 0;
}
要更改变量integer
的值,请将该变量的地址发送到函数test()
现在考虑同样的情况,并考虑是否需要更改本身是
struct节点*
的变量的内容。然后发送该变量的地址,并使用(struct node*)*
接收它。这就是您发布的示例中发生的情况
您希望对
insert()
函数中的leaf
变量所做的更改反映在调用函数中。要实现这一点,您需要发送地址并相应地更改内容。这是通过引用调用的。如果我们必须更改*leaf的值,那么我们应该有它的地址,这就是为什么我们使用两个*作为leaf的指针,另一个用于获取*leaf的地址。案例1:
当您传递一个变量的地址时,一个指针就足以访问该变量
例如:
struct node a;
func(&a); // calling
在func()
定义中:
func(struct node *a);
这里a指向节点的地址。我们可以直接使用它的地址访问a
案例2:
发送指针变量的地址时:
struct node *a;
func(&a); // calling
然后在函数定义中应使用双指针:
func(struct node **a);
在insert(int键,结构节点**leaf)
中,您正在通过C版本的“按引用传递”传递*leaf
所指向的地址。在insert(int-key,struct-node*leaf)
中,通过传递值方法传递*leaf
指向的地址。注意C参数总是通过值传递
因此,在这种特殊情况下,使用insert(int-key,结构节点**leaf)
或insert(int-key,结构节点*leaf)
都可以获得相同的输出。这种情况下唯一的区别是insert(int-key,结构节点**leaf)
您通过引用传递的C版本和在插入(int键,结构节点*leaf)中传递地址
您通过传递值传递地址
方法
示例代码A
#include<stdio.h>
struct node
{
int data;
};
void AddFive(struct node **t);
int main()
{
struct node *n = NULL;
n = new node;
n->data = 5;
printf("%d\n", n->data);
AddFive(&n);
printf("%d\n", n->data);
return 0;
}
void AddFive(struct node **t)
{
(*t)->data = (*t)->data+5;
}
#包括
结构节点
{
int数据;
};
void AddFive(结构节点**t);
int main()
{
结构节点*n=NULL;
n=新节点;
n->data=5;
printf(“%d\n”,n->data);
添加五个(&n);
printf(“%d\n”,n->data);
返回0;
}
void AddFive(结构节点**t)
{
(*t)->数据=(*t)->数据+5;
}
示例代码B
#include<stdio.h>
struct node
{
int data;
};
void AddFive(struct node *t);
int main()
{
struct node *n = NULL;
n = new node;
n->data = 5;
printf("%d\n", n->data);
AddFive(n);
printf("%d\n", n->data);
return 0;
}
void AddFive(struct node *t)
{
t->data = t->data+5;
}
#包括
结构节点
{
int数据;
};
void add5(结构节点*t);
int main()
{
结构节点*n=NULL;
n=新节点;
n->data=5;
printf(“%d\n”,n->data);
加五(n);
printf(“%d\n”,n->data);
返回0;
}
void AddFive(结构节点*t)
{
t->data=t->data+5;
}
如果您注意到代码A和代码B都实现了相同的输出。它是指向结构节点*
的指针。这看起来像一个二叉树,如果是这样,您的左、右节点或“叶”就是子分支。注:*leaf;它不叫“双指针”。这叫做“指针到指针”