简体   繁体   English

为什么在RemoveHead(节点)函数中使用** head(而不是* head)?

[英]Why use **head (and not *head) in RemoveHead(node) function?

In most of the explanations in the book the author insists of using **list passing instead of *list, however as per my understanding I feel there is nothing wrong in *list. 在书中的大多数解释中,作者坚持使用**列表传递而不是* list,但是根据我的理解,我觉得* list中没有任何错误。 Please someone explain me in detail if i am wrong. 如果我错了,请有人详细解释我。 For example, to delete head of a linked list, the author says the below code is wrong. 例如,要删除链表的头部,作者说下面的代码是错误的。

void RemoveHead(node *head)
{
   node *temp = head-> next; /* line 1 */
   free(head);
   head = temp;
}

Instead he says the below code must be used 相反,他说必须使用以下代码

void RemoveHead(node **head)
{
   node *temp = (*head)-> next;
   free(*head);
   *head = temp;
}

In your first example: 在你的第一个例子中:

head = temp;

Does nothing outside the function, it only sets the local variable head to temp inside the function. 在函数外部没有任何功能,它只在函数内部将局部变量headtemp Whatever variable the caller passed in will remain unchanged. 无论调用者传入什么变量都将保持不变。 The author is correct, you need to use pointers or (better) references (if you're using C++). 作者是正确的,你需要使用指针或(更好的)引用(如果你使用的是C ++)。

When passing a node* as the argument, you can free, and modify the contents of the memory address pointed by that pointer. 传递node*作为参数时,可以释放,并修改该指针指向的内存地址的内容 However, modifications performed on the pointer itself will not be seen from outside the function, since the pointer is passed by value. 但是,从函数外部看不到对指针本身执行的修改,因为指针是按值传递的。

On you second example, since you are passing a pointer to the head pointer, you can actually modify the actual head pointer, not just the memory pointed by it. 在第二个例子中,由于您正在传递一个指向头指针的指针,您实际上可以修改实际的头指针,而不仅仅是它指向的内存。

So in your second function, when *head = temp; 所以在你的第二个函数中,当*head = temp; is executed, you are modifying the address to which the pointer passed as argument points to. 执行时,您正在修改指针作为参数指向的地址。

Therefore, the author is right. 因此,作者是对的。

Author is right. 作者是对的。 ** is effectively pass by reference (ie you can change 'head'). **实际上是通过引用传递的(即你可以改变'head')。

In your version (pass by value), head remains unchanged in the calling code. 在您的版本中(按值传递),head在调用代码中保持不变。

Well. 好。 When you want to modify a integer variable inside a function, you pass it by reference. 如果要修改函数内的整数变量,可以通过引用传递它。 That is, you pass its address. 也就是说,你传递了它的地址。

int x = 5;

void modify_value (int* x)
{
    (*x) = 7; // is not 5 anymore
}

With pointers is the same. 用指针是一样的。 If you want to modify a pointer. 如果要修改指针。 You have to pass it by reference. 你必须通过引用传递它。 That is, you have to pass its address. 也就是说,你必须传递它的地址。 Which is a pointer to a pointer. 这是指向指针的指针。

int* ptr_x = &x;

void modify_pointer (int** ptr_x)
{
    *ptr_x = NULL; // is not &x anymore
}

The author is correct. 作者是对的。 In your first example, you're modifying a local copy of head - the caller won't notice that anything has changed. 在您的第一个示例中,您正在修改head的本地副本 - 调用者不会注意到任何更改。 head will still have been freed, so you're on track for a crash in pretty short order. head仍然会被释放,所以你可以在很短的时间内完成碰撞。 You need to pass a pointer to modify the caller's variable. 您需要传递一个指针来修改调用者的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM