如果不通过指向它的指针引用传递指针b,则函数将处理指针值的副本
在函数内更改副本不会影响原始指针。
因此函数会产生内存泄漏。
要更改原始指针b,该函数需要直接访问指针。可以通过解引用指向指针(对象)b的指针来实现。
比较这两个演示程序。
#include <stdio.h>
void f( int *p )
{
++p;
printf( "Within the function f p = %p\n", ( void * )p );
}
int main(void)
{
int x = 10;
int *p = &x;
printf( "Before calling f p = %p\n", ( void * )p );
f( p );
printf( "After calling f p = %p\n", ( void * )p );
return 0;
}
程序输出是
Before calling f p = 0x7ffc3a940cbc
Within the function f p = 0x7ffc3a940cc0
After calling f p = 0x7ffc3a940cbc
如您所见,在 main 中声明的指针 p 在调用函数 f 后没有改变。
还有另一个程序
#include <stdio.h>
void f( int **p )
{
++*p;
printf( "Within the function f p = %p\n", ( void * )p );
}
int main(void)
{
int x = 10;
int *p = &x;
printf( "Before calling f p = %p\n", ( void * )p );
f( &p );
printf( "After calling f p = %p\n", ( void * )p );
return 0;
}
程序输出是
Before calling f p = 0x7ffe011131ec
Within the function f p = 0x7ffe011131f0
After calling f p = 0x7ffe011131f0
现在您可以看到,在调用函数 f 后,main 中声明的指针 p 发生了变化,因为它是通过指向它的指针通过引用传递给函数的。取消对指针的引用,函数可以直接访问 main 中声明的指针 p。
如果一个参数只接受双指针,它怎么能接受一个
地址值
每个值都根据其类型进行解释。此表达式&b 的值具有int ** 类型。并且函数参数也有int **的类型。
您可以想象函数定义及其调用方式如下
int main() {
int* b = NULL;
malloc2(&b);
}
//...
int malloc2( /* int **a */ ) {
int **a = &b;
*a = malloc(sizeof (int));
return 0;
}
即函数参数a被表达式&b的值初始化。
或者更简单的例子
int main( void )
{
int x = 10;
int *b = &x;
int **a = &b;
}