我有一个问题,理解双指针
我正在制作一个程序,将项目添加到LIFO(最后一次)数组中,其中元素在数组开始时插入。
这是我的代码:
typedef struct neud{
int num;
struct neud *next;
}neud;
void add(neud** head,int val){
//creat a new neod
neud* newNeod;
newNeod = malloc(sizeof(neud));
//assigning
newNeod->num = val;
newNeod->next= *head;
//change the head;
*head = newNeod;
}
int main()
{
neud* head = NULL;
add(&head,10);
add(&head,20);
add(&head,30);
return 0;
}
一切正常,但是我不完全理解这里需要双分钟。有人可以解释一下吗?
I'm making a program to add items to a LIFO (last in first out) array, where the elements are inserted at the start of the array.
Here is my code:
typedef struct neud{
int num;
struct neud *next;
}neud;
void add(neud** head,int val){
//creat a new neod
neud* newNeod;
newNeod = malloc(sizeof(neud));
//assigning
newNeod->num = val;
newNeod->next= *head;
//change the head;
*head = newNeod;
}
int main()
{
neud* head = NULL;
add(&head,10);
add(&head,20);
add(&head,30);
return 0;
}
Everything works fine, but I don't understand precisely the need for a double-pointer here. Can someone explain this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑这个简单的程序。
程序输出是
函数f处理传递给该函数的变量x值的副本。因此,相对于原始变量X,更改函数中的副本没有效果。
要更改函数中的原始变量X,您需要通过参考将其传递。在C传递的C中,意味着间接通过指向其指针的对象。因此,请在功能中删除指针,您可以直接访问指针指向对象并可以更改它。
将下面的程序与该程序上方的
程序输出
进行比较,因此,如果您会声明自己的函数
并将其称为函数
,那么该功能将处理原始指针头值的副本。指针本身的价值将保持不变。因此,您需要通过参考将指针传递,该指针应像该功能一样声明
并称为
Consider this simple program.
The program output is
That is the function f deals with a copy of the value of the variable x passed to the function. So changing the copy within the function has no effect relative to the original variable x.
To change the original variable x in the function you need to pass it by reference. In C passing by reference means passing an object indirectly through a pointer to it. Thus dereferencing the pointer within the function you get a direct access to the object pointed to by the pointer and can change it.
Compare the program below with this program above
The program output is
So if you will declare your function like
and call it like
then the function will deal with a copy of the value of the original pointer head. The value of the pointer itself will stay unchanged. So you need to pass the pointer by reference that is the function should be declared like
and called like
在c中,所有参数对函数的参数均通过value 传递。这意味着调用中使用的值已复制到函数 local 参数变量中。
因此,参数变量的行为与任何其他局部变量一样,其寿命随函数的结束而结束。当功能返回时,您对变量的所有更改都将丢失。
现在考虑
head
指针。如果您没有将指针传递给指针,而只需分配给head
:现在将丢失分配。
当您将指针传递给指针时,您会通过参考模拟调用。该功能现在具有指向原始指针的指针。您可以在
main
函数中取消双分球head
head 以获取原始变量 head 。In C all arguments to functions are passed by value. That means the value used in the call is copied into the functions local argument variable.
As such, the argument variable behaves like any other local variable, and its life-time ends with the end of the function. All changes you made to the variable will be lost when the function returns.
Now think about the
head
pointer. If you don't pass a pointer to a pointer, and just assign tohead
:Now that assignment will be lost.
When you pass a pointer to a pointer, you emulate call by reference. The function now has a pointer to the original pointer. You can dereference the double-pointer
head
to get the original variablehead
in themain
function.