变量的指针和cout地址

时间:2016-04-30 17:54:19

标签: c++ pointers cout

我正在尝试cout变量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;

}

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

2 个答案:

答案 0 :(得分:4)

问题在于:

*app=3;

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

如果删除它,看到预期的输出是有意义的。

答案 1 :(得分:2)

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

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