Prerequisite : Pointers in C/C++

**THERE CAN BE MANY REASONS, BUT HERE ARE TWO REASONS:**

**Reason 1 :**

Consider int arr[100]. The answer lies in the fact how the compiler interprets arr[i] ( 0<=i<100).

arr[i] is interpreted as *(arr + i). Now, arr is the address of the array or address of 0th index element of the array. So, address of next element in the array is arr + 1 (because elements in the array are stored in consecutive memory locations), further address of next location is arr + 2 and so on. Going with the above arguments, arr + i mean the address at i distance away from the starting element of the array. Therefore, going by this definition, i will be zero for the starting element of the array because the starting element is at 0 distance away from the starting element of the array. To fit this definition of arr[i], indexing of array starts from 0.

## CPP

`#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; ` `}` |

*chevron_right*

*filter_none*

**Output:**

2 2

The conclusion is, we need random access in the array. To provide random access, compilers use pointer arithmetic to reach i-th element.

**Reason 2 :**

Modern languages, especially **C++** use row-major** **ordering for storing two-dimensional arrays.

Let us assume a 2D array and write a row-major formula with two different approaches:

- array indices starting from 1
- array indices starting from 0

let the 2D array be arr[m][n] of type int

let &arr be “address”

case 1 ( array indices start from 1 ) :

&( arr[i][j] ) = address + [ ( i-1 )*n + ( j-1 ) ]*( sizeof(int) ) ] so here we are performing 6 operations

case 2 ( array indices start from 0 ) :

&( arr[i][j] ) = address + [ ( i )*n + ( j ) ]*( sizeof(int) ) ] and here we are performing only 4 operations

So we see here that we are performing 2 less operations when we store 2D arrays and obtaining an element’s address. This looks like it doesn’t make sense but it does! While handling with huge size data this may improved performance and speed. **case 1** may look user-friendly but **case 2** is more efficient. That’s why most languages like **C++, PYTHON, JAVA** use arrays starting with index 0 and rarely languages like **Lua** arrays starting with index 1.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if sum of array can be reduced to zero by repetitively reducing array element by their index value
- Why is the size of an empty class not zero in C++?
- Rearrange array to make sum of all subarrays starting from first index non-zero
- Maximum length palindromic substring such that it starts and ends with given char
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Queries to answer the number of ones and zero to the left of given index
- Find the index which is the last to be reduced to zero after performing a given operation
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Queries for number of distinct elements from a given index till last index in an array
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Farthest index that can be reached from the Kth index of given array by given operations
- Even numbers at even index and odd numbers at odd index
- Check if every index i has an index j such that sum of elements in both directions are equal
- Number of permutations such that sum of elements at odd index and even index are equal
- Count of contiguous subarrays possible for every index by including the element at that index
- Check if it is possible to reach to the index with value K when start index is given
- Rearrange array to make product of prefix sum array non zero
- Construct array with sum of product of same indexed elements in the given array equal to zero
- Why is it faster to process sorted array than an unsorted array ?
- Minimum operations to make XOR of array zero

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.