在另一个函数中修改指针需要一个叫做多重间接的概念,我会在后面解释,剧透解决方案给出@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(&) 运算符分配它们。
多重间接指针类型变量所做的是,允许我们做的是
指向指针变量本身的指针,用于修复上述程序。
这允许我们将ptrMain 的地址传递给allocater()
使用这个调用
allocater(&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(&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