68

所以,我有一些代码,有点像下面,将结构添加到结构列表中:

void barPush(BarList * list,Bar * bar)
{
    // if there is no move to add, then we are done
    if (bar == NULL) return;//EMPTY_LIST;

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = list;

    // and set list to be equal to the new head of the list
    list = newNode; // This line works, but list only changes inside of this function
}

这些结构定义如下:

typedef struct Bar
{
    // this isn't too important
} Bar;

#define EMPTY_LIST NULL

typedef struct BarList
{
    Bar * val;
    struct  BarList * nextBar;
} BarList;

然后在另一个文件中我执行以下操作:

BarList * l;

l = EMPTY_LIST;
barPush(l,&b1); // b1 and b2 are just Bar's
barPush(l,&b2);

但是,在这之后,l 仍然指向 EMPTY_LIST,而不是在 barPush 内部创建的修改版本。如果我想修改它,我是否必须将列表作为指向指针的指针传递,或者是否需要其他一些黑暗的咒语?

4

6 回答 6

69

如果你想这样做,你需要传入一个指向指针的指针。

void barPush(BarList ** list,Bar * bar)
{
    if (list == NULL) return; // need to pass in the pointer to your pointer to your list.

    // if there is no move to add, then we are done
    if (bar == NULL) return;

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = *list;

    // and set the contents of the pointer to the pointer to the head of the list 
    // (ie: the pointer the the head of the list) to the new node.
    *list = newNode; 
}

然后像这样使用它:

BarList * l;

l = EMPTY_LIST;
barPush(&l,&b1); // b1 and b2 are just Bar's
barPush(&l,&b2);

Jonathan Leffler 建议在评论中返回列表的新负责人:

BarList *barPush(BarList *list,Bar *bar)
{
    // if there is no move to add, then we are done - return unmodified list.
    if (bar == NULL) return list;  

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = list;

    // return the new head of the list.
    return newNode; 
}

用法变为:

BarList * l;

l = EMPTY_LIST;
l = barPush(l,&b1); // b1 and b2 are just Bar's
l = barPush(l,&b2);
于 2009-04-20T04:35:11.040 回答
24

通用答案:将指针传递给您要更改的内容。

在这种情况下,它将是指向要更改的指针的指针。

于 2009-04-21T00:43:59.043 回答
17

请记住,在 C 中,一切都是按值传递的。

你传入一个指向指针的指针,像这样

int myFunction(int** param1, int** param2) {

// now I can change the ACTUAL pointer - kind of like passing a pointer by reference 

}
于 2009-04-20T04:33:38.030 回答
3

这是一个经典的问题。返回分配的节点或使用指针的指针。在 C 中,您应该将指向 X 的指针传递给您希望修改 X 的函数。在这种情况下,由于您希望修改指针,因此您应该将指针传递给指针。

于 2009-04-20T04:35:49.523 回答
2

是的,您必须传入一个指向该指针的指针。C 通过值而不是引用传递参数。

于 2009-04-20T04:33:27.890 回答
2

在另一个函数中修改指针需要一个称为多重间接的概念,我将在后面解释它,由于@geofftnz 使用多重间接的剧透解决方案。我想做的是尽我所能解释C中的多重间接。

考虑以下两个程序,我将遍历代码。

下面的程序没有使用多重间接,所以它失败了。

有错误的程序:

// filename: noIndirection.c
#include <stdio.h>
#include <stdlib.h>

void allocater(int *ptrTempAllctr)
{
    ptrTempAllctr = malloc(sizeof(int));
    if (ptrTempAllctr == NULL) {
        perror("in allocater() memory allocation error");
        exit(EXIT_FAILURE);
    }
}

int main() 
{
    int *ptrMain = NULL;
    allocater(ptrMain);
    if (ptrMain == NULL) {
        printf("ptrMain is points to NULL\n");
        return 1;
    }
    //free(ptrMain);  // we don't have to free because it will be invalid free.
    return 0;
}

考虑上面的 program( noIndirection.c),它有一个变量ptrMain,它是一个指向 int 的指针。如果它被传递给函数,则在函数范围(主体)中创建一个临时指针变量,因为函数的参数是临时变量,当它们超出范围时它们会被删除。

临时指针变量ptrTempAllctr(它是一个参数)将指向 caller( main) 函数的变量ptrMain(它指向NULL)在它作为参数传递给函数时所指向的东西。

如果我们使用malloc()或分配另一个指向临时变量的指针,ptrTempAllctr那么它将指向它,但是main作为参数传递给 to 函数的 caller() 函数中的指针变量仍然指向它之前allocater()指向的相同数据(即)NULL函数调用。

当被调用的 ( allocater()) 函数超出范围时,临时指针变量会从堆栈中弹出,并且内存未分配,我们最终会导致内存泄漏。为了绕过这个限制,我们需要使用多重间接。

多方向:

Multiple indirection when we use of pointer/s to pointer/s in varying level(with multiple `*`) eg: `int **pp, int ***ppp`, etc.

&我们使用 address-of( ) 运算符分配它们。

多重间接指针类型变量所做的是,允许我们制作一个指向指针变量本身的指针,用于修复上述程序。这允许我们 使用此调用将地址传递ptrMainallocater()

allocater(&ptrMain);

因此上面的程序noIndirection.c不允许我们这样做,请参阅withIndirection.c实现这种多重间接的程序。

在这种情况下,我们需要指向 int 指针(int **ptrMain)的指针作为函数的函数参数allocater()来解决上述错误程序(noIndirection.c)。

这在以下程序中使用。

以下程序使用多重间接来解决之前程序中的错误。

// filename: withIndirection.c
#include <stdio.h>
#include <stdlib.h>

void trueAllocater(int **ptrTrueAllocater)
{
    *ptrTrueAllocater = (int *) malloc(sizeof(int));
    if (ptrTrueAllocater == NULL) {
        perror("in trueAllocater() memory allocation error");
        exit(EXIT_FAILURE);
    }
}

int main(void) 
{
    int *ptrMain = NULL;
    trueAllocater(&ptrMain);
    if (ptrMain == NULL) {
        printf("memory not allocated\n");
        return EXIT_FAILURE;
    }

    printf("memory allocated and assigned to ptrMain");
    printf(" from trueAllocater\n");

    free(ptrMain);
    return EXIT_SUCCESS;
}

从现在开始查看程序withIndirection.c以供参考。

为了解决我们的问题,我们需要将指针变量ptrMain(间接的,即在我目前对传递的变量的理解中,将另一个 * 添加到参数声明中。trueAllocater(&ptrMain);ptrMaintrueAllocater()

通过这种方式,我们需要将trueAllocater()函数参数设置为int **from int *inwithIndirection.c而不是noIndirection.c 这样才能统计间接级别。

当调用者的参数变量ptrMain的实际地址被传递给函数时。函数中的临时参数变量指向调用者()函数ptrTrueAllocater中指针变量的地址,而不是函数()中的指针变量(在程序中)指向的地址。ptrMainmainptrMainNULLmain

如果我们取消引用变量,指向ptrTrueAllocater的地址将被显示,因为临时变量指向 caller( )变量本身,而不是它的内容。ptrMainptrTrueAllocatermainptrMain

解引用ptrTrueAllocater变量的内容将是调用者(main)的变量(ptrMain)所指向的数据的地址,因此我们必须进行一次额外的解引用才能获得最终数据。

所以我们必须取消引用一次以获得ptrMain它指向的地址,以便更改ptrMain需要指向的位置并取消引用两次以获得由ptrMainwhich is指向的实际数据NULL

@PaulWicks 您打算更改,因此您必须取消引用一次才能分配或更改其指向的位置。

使用指针进行多重间接的目的是创建多维数组并传递需要指向某物的指针参数。

我们需要根据我们必须操作的类型来更改变量,如下所示,

在声明中每次添加 * 都会增加指针间接级别,每次取消引用都会降低接近数据的指针间接级别。

我们可以通过将地址返回给分配给所需指针变量的调用函数来解决这个问题。

是的,我们可以使用这种多间接变量语法来创建一维或多维数组。这首先会使初学者感到困惑,如果他们花时间阅读大量代码,他们将能够找到它们之间的区别。

如果我错了,请纠正我,请提供反馈并让我知道多个间接指针的其他用途是什么。为我糟糕的英语道歉。这些是帮助我理解多种间接方式的资源。 https://boredzo.org/pointers/#function_pointers https://cseweb.ucsd.edu/~ricko/rt_lt.rule.html

于 2021-06-02T16:08:17.083 回答