数组的索引大小

Ana*_*rba -1 c++

如果我有一个数组,a[100]并且我从1或开始0。当我声明时int a[100],我的数组多长时间?

它总是从开始计数0?如果是,它将是一个带有101空格的数组。

#include <iostream>

using namespace std;

int main()
{
    float time[20]; //
    int a, first = 20, y, x;
    for (x = 1; x < 21; x++) {
        cout << "Enter the time of the person number " << x << " : ";
        cin >> time[x];
    }
    for (y = 1; y < 20; y++) {
        if (time[y] < first) {
            first = time[y];
            a = y;
        }
    }
    getchar();
    cout << "The person " << a << " was the faster.";
    cout << time[1];
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

从这里开始1

如果我更改,它将从开始0

for (x=0;x<21;x++)

for (y=0;y<20;y++)
Run Code Online (Sandbox Code Playgroud)

为什么要开始更好0呢?

Jar*_*d42 5

C ++使用0索引,因此int a[20];有效索引为0,..,19。

a[20] 确实超出了访问范围,导致了UB。