【问题标题】:How do I modify a pointer that has been passed into a function in C?如何修改已传递给 C 函数的指针?
【发布时间】:2017-07-25 10:36:23
【问题描述】:

所以,我有一些类似于下面的代码,用于将结构添加到结构列表中:

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 内部创建的修改版本。如果我想修改它,我是否必须将列表作为指向指针的指针传递,或者是否需要其他一些黑暗的咒语?

【问题讨论】:

    标签: c function pointers parameters pass-by-value


    【解决方案1】:

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

    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 建议在 cmets 中返回新的列表头:

    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);
    

    【讨论】:

    • 谢谢,我认为这是问题所在,但希望不是;)
    • 或者,让函数返回指向新列表头的指针。 BarList *barPush(BarList *list, Bar *bar)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      int myFunction(int** param1, int** param2) {
      
      // now I can change the ACTUAL pointer - kind of like passing a pointer by reference 
      
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            在另一个函数中修改指针需要一个叫做多重间接的概念,我会在后面解释,剧透解决方案给出@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;
            }
            

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

            临时指针变量ptrTempAllctr(这是一个参数)将指向调用者(main)函数的变量ptrMain(指向NULL)在作为参数传递给函数。

            如果我们使用malloc() 或分配另一个指向临时变量ptrTempAllctr 的指针,那么它将指向它,但调用者(main) 函数中的指针变量作为参数传递给to 函数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(&amp;) 运算符分配它们。

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

            allocater(&amp;ptrMain);

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

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

            这在以下程序中使用。

            下面的程序使用多重间接来解决前面程序的bug。

            // 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(&amp;ptrMain);) 的地址传递给trueAllocater,以便将ptrMain 更改为稍后在trueAllocater() 或其他中需要指向的位置功能, 为此,该函数需要接受具有正确间接级别的间接指针, 这是在我目前对传递的变量的理解中添加另一个 * 添加到参数声明中。

            通过这种方式,我们需要将trueAllocater() 函数参数作为int ** 来自int * in withIndirection.c 而不是noIndirection.c 因此间接级别将被统计。

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

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

            取消引用的ptrTrueAllocater变量的内容将是调用者(main)的变量(ptrMain)所指向的数据的地址, 所以我们必须做一个额外的取消引用才能获得最终数据。

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

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

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

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

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

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

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-03-01
              • 2020-12-10
              • 2011-05-19
              • 2013-09-17
              相关资源
              最近更新 更多