gpt4 book ai didi

c - 圆括号内的指针

转载 作者:太空狗 更新时间:2023-10-29 15:22:28 25 4
gpt4 key购买 nike

void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}

如果我没记错的话,在指针上加上括号意味着调用函数?如果那是真的,我真的不明白为什么 *head_ref 上有方括号。我喜欢解释一下为什么我需要在这段代码中的 *head_ref 上加上括号。

最佳答案

在这种特殊情况下,括号除了阐明程序员的意图外没有任何作用,即他们想要取消引用 head_ref

注意head_ref是指向指针的指针,所以在这种情况下,new_node->next被设置为指向链表的原始头部,然后 head_ref 指向的指针被更新为指向 new_node,它现在是列表的开头。

正如 Michael Krelin 在下面指出的那样,将指针放在方括号中并不意味着它是调用函数或指向函数的指针。如果您看到这样的代码:(*head_ref)() then 这将是对 head_ref 指向的函数的调用。

关于c - 圆括号内的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com