5

why arrays first index starts with 0

1

6 Answers 6

5

Because index actually means the offset from the pointer. The offset of the first element is 0.

Update upon comment Well, I'll try.

Let's consider an array of bytes with 10 elements:

byte array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Consider the memory cells where this array is located (lets assume it starts from the address 0010h):

   0010  0011  0012  0013  0014  0015  0016  0017  0018  0019
  +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10 |
  +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

Our variable array points to 0010h.

The offset of 1 (the first element) is 0 and its actual address is 0010 + 0 (where 0010 is the address of array and 0 is the offset).

The offset of 3 (the third element) is 2 because it is in the third cell, and the cell size is 1 (because we have byte array). the 3rd element's actual address is 0010 + 2.

Coming back to our programming language: array[0] means the content of the memory cell which has the 0010 address, array[1] means the content of the memory cell with the 0010 + 1 address (the second element) and so on. *array in C refers to the first element, *(array+1) - to the second.

Sign up to request clarification or add additional context in comments.

Comments

2

The same reason that mathematically-minded people call tomorrow "one day from now" rather than "two days from now".

Comments

1

Because pointer arithmetics are easier that way.

In C you can treat arrays as pointers. That way, if you have this array:

char a[50];
char *ptr = a;

Using pointer arithmetics, you can move the pointer by adding integers to it this way:

*(ptr + 0); // first character in the array
*(ptr + 1); // second character in the array
// so on, so forth

Comments

0

Because element 0 is at a distance of 0 from the beginning of the array. It's the first element but it's at position 0, just like the way that there are no years greater than 100 in the first century.

Comments

0

First and the most important: Because standard/your compiler says so. Secondly, because of the pointer arithmetic. If you have an array A then A + 0 is the first element, A + 1 second and so on. It is the way array is represented in the memory.

Comments

0

The first index always starts with zero because of the way the compilers use the value of an index to calculate the actual address of an element in the array.

Consider the following example.

void someFunction()
{
    int exampleArray[5] = {0, 1, 2, 3, 4};
}

"exampleArray", needs space to store 5 elements, and each element will require the amount of space required to store a data type "int". Depending upon the platform (16bit/32bit or 64bit) and the OS, the size of "int" data type might be different. For our discussion, lets say that size of "int" is 4 bytes.

Thus to store the above array in memory, we need memory for 5 integers = 5 * 4 = 20 bytes. Now every element in this array has an "address".

Lets say "exampleArray" is stored at address 100. Then the address of the element at an index "i" will be 100 + (i * sizeof(int)). So for the above array,

Index 0, address = 100 + (0 * 4) = 100
Index 1, address = 100 + (1 * 4) = 104
Index 2, address = 100 + (2 * 4) = 108
Index 3, address = 100 + (3 * 4) = 112
and so on..

This is how the compiler generates code for accessing array indices. So when we write exampleArray[2], the computer needs to be able to calculate the address of the element at index 2, to be able to access its memory location, and thus access its value. So as per the above formula, element at index "2" will be 100 + (2 * 4) = 108.

Since the 1st element in the array will have to be at address 100, its index becomes 0 so as to calculate its address in a simple manner.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.