[英]Linked list and double pointers
In the below code, I am trying to insert a node after a particular node. 在下面的代码中,我尝试在特定节点之后插入一个节点。 In the function, I will be giving as input the address of the previous node after which I want to insert the new node.
在函数中,我将输入前一个节点的地址作为输入,然后在该地址之后插入新节点。 The problem is in the 10th line of function insertAfter() - it says I cannot access *prev_ref->next.
问题出在函数insertAfter()的第十行中-它说我无法访问* prev_ref-> next。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void push(struct node **head_ref, int data)
{
struct node* newNode = (struct node*)malloc(sizeof(struct node)) ;
newNode->next= *head_ref;
newNode->data= data;
*head_ref= newNode;
}
void insertAfter(struct node **prev_ref, int data)
{
if(*prev_ref==NULL)
{
printf("prev ref cant be null");
return;
}
struct node * newNode;
newNode = (struct node*)malloc(sizeof(struct node)) ;
newNode->next= *prev_ref->next;
newNode->data= data;
*prev_ref->next= newNode;
}
void printList(struct node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}
main()
{
struct node* head = NULL;
push(&head, 7);
push(&head, 1);
insertAfter(&head, 8);
printf("\n Created Linked list is: ");
printList(head);
getchar();
return 0;
}
Do you know (*p).s
is equivalent to p->s
? 你知道
(*p).s
等于p->s
吗? I would suggest you to try something like (*prev_ref)->next
or (**prev_ref).next
我建议您尝试类似
(*prev_ref)->next
或(**prev_ref).next
You seem to dereference prev_ref
three-levels deep instead of two. 您似乎取消引用了
prev_ref
三个级别,而不是两个级别。
pointer->field
is dereference of a pointer, equivalent to (*pointer).field
pointer->field
是指针的取消引用,等效于(*pointer).field
So, **prev_ref->next;
因此,
**prev_ref->next;
is in fact (***prev_ref).next;
实际上是
(***prev_ref).next;
Either drop one asterisk or use .
放一个星号或使用
.
instead of ->
. 代替
->
。
EDIT: You seem to have skipped the parentheses we included in our answers. 编辑:您似乎已经跳过了我们在答案中包含的括号。
->
has higher precedence than *
. ->
具有比*
高的优先级。
The effect is: 效果是:
(*prev_ref)->next
prev_ref
(let's call it memory location A
), prev_ref
指向的内存(我们称其为内存位置A
), A
, let's call it B
, A
指向的内存,我们称它为B
, next
field of the structure, offset by a set distance from B
, let's call it C
next
字段的位置,该位置与B
偏移了设定的距离,我们称其为C
Now for *prev_ref->next
现在用于
*prev_ref->next
->
and finds the memory pointed to by prev_ref
(A), just the same ->
并找到prev_ref
(A)指向的内存,就像 next
field of the structure, offset by a set distance from A
, which happens to be an entirely random location in memory (because A stored a pointer to the structure, not the structure itself); next
字段的位置,与A
偏移一个设定的距离,该位置恰好是内存中的一个完全随机的位置(因为A存储了指向该结构的指针,而不是结构本身); let's call that location D. Now, the system won't let you do that, because it sees A is not where a structure lies, but where a pointer to a structure lies, hence the error message 现在,系统不允许您这样做,因为它看到A不是结构所在,而是指向结构的指针,因此出现错误消息
And the fundamental reason of your problems is that you use pointers-to-pointers for no good reason. 问题的根本原因是您没有充分的理由使用指针到指针。 Nothing of this would have happened if you always used plain pointers.
如果您始终使用普通指针,则不会发生任何事情。
void push(struct node *head_ref, int data)
, void insertAfter(struct node *prev_ref, int data)
, prev_ref->next
etc. Managing pointers to pointers is tricky, error-prone (as you've experienced) and in 99% cases completely unnecessary. void push(struct node *head_ref, int data)
, void insertAfter(struct node *prev_ref, int data)
, prev_ref->next
等。管理指针的指针非常棘手,容易出错(如您prev_ref->next
),在99 %的情况完全没有必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.