【发布时间】:2021-01-27 07:08:15
【问题描述】:
我是 C++ 编程新手。我只是想知道为什么这段代码有效,给我实际的数字:
#include <iostream>
using namespace std;
int main(){
const int ARRAYSIZE = 10;
int inc = 0;
int arrayy[ARRAYSIZE];
while (inc < ARRAYSIZE) {
arrayy[inc]=inc;
std::cout << arrayy[inc]<< endl;
inc++;
}
}
但是这段代码为我的数组中的值提供了随机数:
#include <iostream>
using namespace std;
int main(){
const int ARRAYSIZE = 10;
int inc = 0;
int arrayy[ARRAYSIZE];
while (inc < ARRAYSIZE) {
arrayy[inc]=inc;
inc++;
std::cout << arrayy[inc]<< endl;
}
}
【问题讨论】:
-
在第二种情况下,您不会打印放入数组中的值,而是打印数组中下一个插槽中的值,这可能是任何内容。