变量的指针和cout地址

Pointers and cout address of a variable

本文关键字:cout 地址 指针 变量      更新时间:2023-10-16

我正在尝试计算变量abc的地址,而程序刚好使崩溃

int main()
{
    int *app;
    int abc = 2;
    *app=3;
    cout << *app << endl << &*app << endl << abc;
    cout << &abc;
}

然而,如果我删除地址变量int*app,那么它将定制abc 的地址

int main()
{
    int abc = 2;

    cout << &abc;
}

我不知道为什么另一个不相关的地址变量的存在会影响它。请告知。

问题就在这里:

*app=3;

这可能会导致分段错误未定义的行为

如果删除它,则可以看到预期的输出。

你想做什么??这是未定义的行为。

int *app;  // pointer is not initialized.
int abc = 2;
*app=3;  // de-referencing an uninitialized pointer.  Bad.