Ami*_*nos 40 c c++ variable-declaration
此代码编译,但我在Visual Studio中有运行时错误:
运行时检查失败#3 - 正在使用变量'x'而未初始化...
int x = 15;
int main()
{
int x = x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白这种行为...当我点击继续时错误框中的程序恢复,x有一个损坏的内容(比如-8556328代替15).
为什么这段代码没有问题,并且int数组声明得很好?
const int x = 5;
int main()
{
int x[x] = {1,2,3,4};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Bo *_*son 35
声明新变量时,其名称在此处可见
int x =
// ^- there
Run Code Online (Sandbox Code Playgroud)
因为在那一点上变量是完全声明的,因此; 它的名字意味着什么.此时,将隐藏周围范围中的任何其他(先前声明的变量).
小智 5
C中没有范围解析运算符,因此您可能无法使用
int x = x;
Run Code Online (Sandbox Code Playgroud)
在你的程序中.