有时也会使用双指针通过引用将指针传递给函数

Ton*_*ony 1 c pointers

"有时也会使用双指针通过引用传递函数指针"有人可以解释一下上面的语句,究竟是什么指向函数引用的意思?

Nav*_*een 5

我相信这个例子更清楚:

//Double pointer is taken as argument
void allocate(int** p,  int n)
{
    //Change the value of *p, this modification is available outside the function
    *p = (int*)malloc(sizeof(int) * n);
}

int main()
{

    int* p = NULL;

    //Pass the address of the pointer
    allocate(&p,1);

    //The pointer has been modified to point to proper memory location
    //Hence this statement will work
    *p=10;

    //Free the memory allocated
    free(p);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)