更改按值传递的指针 [英] change pointer passed by value

查看:71
本文介绍了更改按值传递的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给了一个函数foo(struct node *n),其中n是链表中的头节点. 现在foo应该更改ns.t.它指向列表的末尾.

I have given a function foo(struct node *n) where n is the head node in a linked list. Now foo should change n s.t. it points to the end of the list.

但是这个函数签名有可能吗?

But is this possible with this function signature?

假设t是指向列表末尾的指针:

Assuming t is the pointer to the end of the list:

  • n = t将不起作用,因为指针是按值传递的.
  • *n = *t不起作用,因为我会覆盖列表的开头.
  • n = t won't work because the pointer is passed by value.
  • *n = *t won't work because I would overwrite the head of the list.

我错过了什么吗?

推荐答案

您将需要使用指向指针的指针:

You would need to use a pointer to a pointer:

foo(struct node **n) 

要更改n指向的内容,请执行以下操作:

To change what n points to, you do:

*n = t;

这篇关于更改按值传递的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆