【发布时间】:2021-11-22 00:13:05
【问题描述】:
我正在学习数据结构,这里有一个我无法理解的东西......
int end(struct node** p, int data){
/*
This is another layer of indirection.
Why is the second construct necessary?
Well, if I want to modify something allocated outside of my function scope,
I need a pointer to its memory location.
*/
struct node* new = (struct node*)malloc(sizeof(struct node));
struct node* last = *p;
new->data = data;
new->next = NULL;
while(last->next !=NULL){
last = last ->next ;
}
last->next = new;
}
- 为什么我们使用 struct node **p?
- 我们可以使用 struct node *p 代替 struct node **p 吗? 我在这里写的评论是我在这里找到的答案,但是我仍然不清楚这里是完整的代码...
请帮帮我 谢谢
【问题讨论】:
-
您确定您正确复制了此代码吗?事实上,这似乎是错误的。而且..因为它不需要双指针。我认为类似:
if (*p == NULL) { ...}不见了。 -
这不是一个完整的代码它只是我感到困惑的功能
-
以防万一您不知道:添加信息和澄清问题的最佳方法是edit您的问题。例如。做我的回答评论中讨论的事情。
-
顺便说一句,你只能接受一个问题(我看到你先接受了我的问题,然后是另一个)。您可以选择对您最有帮助的一个,这就是它的重点。我仅在您认为“勾号”表示“适用/正确”的情况下才提及这一点。它的意思是“最有帮助的答案”。有时很难选择。见stackoverflow.com/help/someone-answers你根据个人意见选择。对我好。 :-)
标签: c data-structures struct