【发布时间】:2023-03-23 20:22:01
【问题描述】:
struct node
{
int data;
struct node *link;
};
struct node *addnode(struct node **head);
int main()
{
struct node *head = NULL;
addnode(&head);
return 0;
}
struct node *addnode(struct node **ptrTohead)
{
if (*ptrTohead == NULL)
{
struct node *newNode = (struct node*) malloc(sizeof(struct node));
*ptrTohead = newNode;
}
}
我正在用 C 语言实现链表并遇到了这段代码:我不明白 &head 的类型是 struct node ** 毕竟 *head 是一个存储地址的指针,而 @987654325 @ 获取头变量的地址。那么它是一个指向指针的指针呢?
这就是我的想象:
// head ----> |___2______|
/memory address/ 100 200
// &head is 100 and is of type struct node *
【问题讨论】:
-
head是一个struct node*并获取head的地址会得到指向struct node*的指针,这是一个struct node**(必须有一个重复?) -
您刚刚描述了如何拥有一个指针,并且您将指针指向它。那怎么不是指向指针的指针?
-
"
*head是一个指针" ...不,head是一个指针。*head取消引用该指针(并且是struct node类型)。指针的地址是指向指针的指针。 -
所以基本上取
head的地址,给出一个指针的地址(head)和head指向某物,因此将这两个想法结合在一起意味着它是一个结构节点** ?我是否正确地考虑了这一点,这太令人困惑了哈哈 -
是的。对于任何类型
T,指向T的指针是T*。指向T*的指针是T**。
标签: c struct dereference pointer-to-pointer