我有一个问题,理解双指针

发布于 2025-02-01 11:50:20 字数 584 浏览 2 评论 0原文

我正在制作一个程序,将项目添加到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2025-02-08 11:50:20

考虑这个简单的程序。

#include <stdio.h>

void f( int x )
{
    x = 10;
}

int main( void )
{
    int x = 0;

    f( x );

    printf( "x = %d\n", x );
}

程序输出是

x = 0

函数f处理传递给该函数的变量x值的副本。因此,相对于原始变量X,更改函数中的副本没有效果。

要更改函数中的原始变量X,您需要通过参考将其传递。在C传递的C中,意味着间接通过指向其指针的对象。因此,请在功能中删除指针,您可以直接访问指针指向对象并可以更改它。

将下面的程序与该程序上方的

#include <stdio.h>

void f( int *px )
{
    *px = 10;
}

int main( void )
{
    int x = 0;

    f( &x );

    printf( "x = %d\n", x );
}

程序输出

x = 10

进行比较,因此,如果您会声明自己的函数

void add(neud * head,int val);

并将其称为函数

add( head, 10 );

,那么该功能将处理原始指针头值的副本。指针本身的价值将保持不变。因此,您需要通过参考将指针传递,该指针应像该功能一样声明

void add(neud** head,int val);

并称为

add( &head, 10 );

Consider this simple program.

#include <stdio.h>

void f( int x )
{
    x = 10;
}

int main( void )
{
    int x = 0;

    f( x );

    printf( "x = %d\n", x );
}

The program output is

x = 0

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

#include <stdio.h>

void f( int *px )
{
    *px = 10;
}

int main( void )
{
    int x = 0;

    f( &x );

    printf( "x = %d\n", x );
}

The program output is

x = 10

So if you will declare your function like

void add(neud * head,int val);

and call it like

add( head, 10 );

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

void add(neud** head,int val);

and called like

add( &head, 10 );
面如桃花 2025-02-08 11:50:20

在c中,所有参数对函数的参数均通过value 传递。这意味着调用中使用的值已复制到函数 local 参数变量中。

因此,参数变量的行为与任何其他局部变量一样,其寿命随函数的结束而结束。当功能返回时,您对变量的所有更改都将丢失。

现在考虑head指针。如果您没有将指针传递给指针,而只需分配给head

void add(neud* head,int val){
    // ...
    head = newNeod;
}

现在将丢失分配。

当您将指针传递给指针时,您会通过参考模拟调用。该功能现在具有指向原始指针的指针。您可以在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 to head:

void add(neud* head,int val){
    // ...
    head = newNeod;
}

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 variable head in the main function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文