在另一个函数中修改指针需要一个称为多重间接的概念,我将在后面解释它,由于@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( ) 运算符分配它们。
多重间接指针类型变量所做的是,允许我们制作一个指向指针变量本身的指针,用于修复上述程序。这允许我们
使用此调用将地址传递ptrMain
给allocater()
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);
ptrMain
trueAllocater()
通过这种方式,我们需要将trueAllocater()
函数参数设置为int **
from int *
inwithIndirection.c
而不是noIndirection.c
这样才能统计间接级别。
当调用者的参数变量ptrMain
的实际地址被传递给函数时。函数中的临时参数变量指向调用者()函数ptrTrueAllocater
中指针变量的地址,而不是函数()中的指针变量(在程序中)指向的地址。ptrMain
main
ptrMain
NULL
main
如果我们取消引用变量,指向ptrTrueAllocater
的地址将被显示,因为临时变量指向 caller( )变量本身,而不是它的内容。ptrMain
ptrTrueAllocater
main
ptrMain
解引用ptrTrueAllocater
变量的内容将是调用者(main
)的变量(ptrMain
)所指向的数据的地址,因此我们必须进行一次额外的解引用才能获得最终数据。
所以我们必须取消引用一次以获得ptrMain
它指向的地址,以便更改ptrMain
需要指向的位置并取消引用两次以获得由ptrMain
which is指向的实际数据NULL
。
@PaulWicks 您打算更改,因此您必须取消引用一次才能分配或更改其指向的位置。
使用指针进行多重间接的目的是创建多维数组并传递需要指向某物的指针参数。
我们需要根据我们必须操作的类型来更改变量,如下所示,
在声明中每次添加 * 都会增加指针间接级别,每次取消引用都会降低接近数据的指针间接级别。
我们可以通过将地址返回给分配给所需指针变量的调用函数来解决这个问题。
是的,我们可以使用这种多间接变量语法来创建一维或多维数组。这首先会使初学者感到困惑,如果他们花时间阅读大量代码,他们将能够找到它们之间的区别。
如果我错了,请纠正我,请提供反馈并让我知道多个间接指针的其他用途是什么。为我糟糕的英语道歉。这些是帮助我理解多种间接方式的资源。
https://boredzo.org/pointers/#function_pointers
https://cseweb.ucsd.edu/~ricko/rt_lt.rule.html