在C中,在函数的参数中double Asterisk **做什么
我在链接列表中遇到了一个代码片段,但这是我第一次看到参数中使用的“ **”双星号。它做什么?
// Adding an item to the beginning of the list
void push(node_t ** head, int val) {
node_t * new_node;
new_node = (node_t *) malloc(sizeof(node_t));
new_node->val = val;
new_node->next = *head;
*head = new_node;
}
I came across a code snippet in linked list but this is the first time I see "**" double asterisk used in parameters. What does it do?
// Adding an item to the beginning of the list
void push(node_t ** head, int val) {
node_t * new_node;
new_node = (node_t *) malloc(sizeof(node_t));
new_node->val = val;
new_node->next = *head;
*head = new_node;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您具有类似的变量
,并且想在函数中更改它,则需要通过参考将其传递给该函数。
在C传递的C中,意味着间接通过指针向其传递变量。因此,将指针在功能中的指针中取消您可以直接访问原始变量并可以更改它。
这是一个演示程序。
程序输出是
如果变量具有类型类型,例如
,如果您要在函数中更改其值,则需要通过引用将其传递给该函数,
以便将相应的函数参数像
和将指针指向原始指针
head
,就像您可以更改Main中定义的原始指针 head 。
如果您不会通过参考将指针传递,而是按值传递,则该函数将处理原始指针值的副本。更改副本不会影响原始指针。
传递给函数
考虑前面的演示程序,在这种情况下,原始变量通过值将原始变量x 。它是其价值的副本,它在功能中已更改。因此程序输出将是
If you have a variable like for example
and want to change it in a function you need to pass it to the function by reference.
In C passing by reference means passing a variable indirectly through a pointer to it. Thus dereferencing the pointer within the function you have a direct access to the original variable and can change it.
Here is a demonstration program.
The program output is
If a variable has a pointer type like for example
then again if you are going to change its value in a function you need to pass it to the function by reference as for example
So the corresponding function parameter will be declared like
And dereferencing the pointer to the original pointer
head
likeyou can change the original pointer
head
defined in main.If you will not pass the pointer by reference but pass it by value then the function will deal with a copy of the value of the original pointer. Changing the copy does not influence on the original pointer.
Consider the preceding demonstration program where the original variable is passed to the function by value
In this case the original variable x will not be changed. It is a copy of its value that was changed within the function. So the program output will be
在这种情况下,它声明指向
node_t
指针的指针,这是函数参数。这是一个使用字符指针的简单示例:
输出:
In this case, it declares a pointer to a
node_t
pointer, which is a function parameter.Here is a simple example using character pointers:
Output:
在这种情况下,这是因为该功能正在更新指针值。
请记住,要更新参数,我们必须将指针传递给该参数:
现在,让我们用指针类型
p *p *
p *:该行为正是同样 - 我们通过表达式
*ptr
更新var
的值,所有更改的都是类型。In this case, it’s because the function is updating the pointer value.
Remember that for a function to update a parameter, we must pass a pointer to that parameter:
Now, let’s replace
T
with the pointer typeP *
:The behavior is exactly the same - we’re updating the value of
var
through the expression*ptr
, all that’s changed are the types.