“双指针有时也用于通过引用将指针传递给函数” 有人能给我解释一下上面的说法吗,point to function by reference 到底是什么意思?
最佳答案
我相信这个例子更清楚:
//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;
}
关于c - 双指针有时也用于通过引用将指针传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3888020/