小弟正在自学C++,学到了指针数组,数组指针,生命周期等等。 自己突发奇想,写了段代码跑跑。发现了奇怪的现象。
代码如下
#include <iostream>
#include <typeinfo>
#include <type_traits>
using namespace std;
int main ()
{
int a[3] = {1,2,3};
int b[3] = {};
for (int i = 0; i>=0 && i<=2; i++)
{
a[i] = a[i] + 1;
//int b[i];
b[i] = a[i];
cout << a[i] <<endl;
cout << b[i] <<endl;
}
cout << b[0] <<endl;
cout << b[1] <<endl;
cout << b[2] <<endl;
}
运行结果呢是 2 2 3 3 4 4 2 3 4
可是如果我把for循环里面int b[i]前面的注释去掉,结果如下 2 2 3 3 4 4 0 0 0
请问大大们,这是为啥?