变量的指针和 cout 地址
Pointers and cout address of a variable
我正在尝试计算变量 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.
我正在尝试计算变量 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.