samedi 15 juillet 2017

Array - Last index

In an array, the index of the first element is zero. So an array declared as:

int a[5];

should have a[4] as the last element. However, when I execute the following code:

int n;
cin>>n;
int a[n];
for(int i = 0; i++<n; cin>>a[i]);
for(int i = 0; i++<n; cout<<i<<" "<<a[i]<<endl);

And give the input as:

5 1 2 3 4 5

The output is:

1 1
2 2
3 3
4 4
5 5

That implies that a[5] = 5. I was wondering as to how that is possible.

Aucun commentaire:

Enregistrer un commentaire