c - 双指针有时也用于通过引用将指针传递给函数

标签 c pointers

“双指针有时也用于通过引用将指针传递给函数” 有人能给我解释一下上面的说法吗,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/

相关文章:

c++ - 是否有默认设置为 -1>>1 != -1 的 C99 编译器?

c - 在 C 语言中,为什么数组的地址等于它的值?

c - 如何删除链表中的最后一项?

c - 使用 Park&Miller RNG 制作大样本?

c - 释放指向 'double' 值的指针

c - 如何在 C 中将 realloc 与双字符指针一起使用?

c - 菜单循环中 scanf 的奇怪行为

c++ - 在这种情况下使用多个条件运算符是个好主意吗?

c - 我正在为纸牌游戏编写代码。在我的交易卡功能中,程序卡住了,我无法确定原因

c - 在 C 中,在 pthread_create 中使用函数名时是否与使用引用相同?