首页 > 解决方案 > 当指针作为指向另一个函数内的指针的指针传递时会发生什么?

问题描述

那么有人可以解释如果我通过 ...*p 作为 foo 函数的参数名称会发生​​什么

int main()

{

    int i = 10;
    int *const p = &i;
    foo(&p);
    printf("%d\n", *p);

}

void foo(int **p)

{
    int j = 11;
    *p = &j;
    printf("%d\n", **p);

}

标签: c++c

解决方案


Don't do that. You'll have a pointer to an undefined memory location on the stack. Any other function call between foo(&p); and printf("%d\n", *p); is bound to overwrite that memory location with new data.


推荐阅读