Why does the array index start from 0 in programming languages?

 What is an array

An array is a collection of similar types of data item that means it stores the same type of data item.

Consider int arr[100]. The appropriate response lies in the reality how the compiler deciphers arr[i] ( 0<=i<100).

arr[i] is deciphered as *(arr + I). Presently, arr is the location of the exhibit or address of 0th file component of the cluster. Along these lines, address of next component in the exhibit is arr + 1 (since components in the cluster are put away in successive memory areas), further location of next area is arr + 2, etc. Going with the above contentions, arr + I mean the location at I distance away from the beginning component of the cluster. Along these lines, passing by this definition, I will be zero for the beginning component of the exhibit in light of the fact that the beginning component is at 0 separation away from the beginning component of the cluster. To fit this meaning of arr[i], ordering of exhibit begins from 0.

#include<iostream>

using namespace std;

int main()

{

int arr[] = {1, 2, 3, 4};

// Below two statements mean same thing

cout << *(arr + 1) << " ";

cout << arr[1] << " ";

return 0;

}

Comments

Popular posts from this blog

proassignment help backlink data index 41

programming shark backink data index 31

Programming shark backlink data index 67