Why is the first index of the array (list) 0?

Why is the first index of the array (list) 0?

To get the first item from an array(list) by index, we need to use index 0.

But why it is 0 and not 1?

HistoryPermalink

In the old days of programming, when programmers had to deal with memory allocation and assembler language, people started to develop higher-level languages.

Higher-level language in those days meant that we didn't have to write assembler (which could have different instructions for different processors). So by higher-level language here, we mean something like C.

How memory looksPermalink

We can imagine memory as a rectangle divided into equally big parts where each part has its number.

Or it can be like a small town with the shape of a rectangle where each house has its number.

It looks something like this: How physical memory looks

  • Each box represents one byte.
  • One byte can store a number up to 255.
  • One byte = 8bits. 8 bits means it has eight places for 1s and 0s.

For example, this is a one byte representing the number 25: 00011001

For bigger numbers, we need more bytes, e.g. for number 1776, we need 2 bytes, and it would look like this: 0000011011110000

Memory is usually our RAM. Each byte in memory has its address. Memory is linear, so the further we go, the bigger its address. Exactly like with streets.

How does array (list in Python) work?Permalink

When we want to create an array in C, we first need to allocate memory.

The question is, how much memory do we need?

To allocate memory for an array, we need to know two things:

  • How many items do we want to store in an array.
  • What kind of items (values) do we want to store in an array - different values have different sizes, e.g. int (−32,767, +32,767) takes 2 bytes (16 bits), but long takes 4 bytes (32 bits).

So if we want an array for three ints, we need to allocate 6 bytes, but if we want an array for two longs, we need to allocate 8 bytes.

So now that we know how much memory we need, we can go ahead and allocate it.

What we get back from the function which allocates the memory (malloc()) is a pointer to the physical address in memory. And we store it in a variable.

So our variable, let's call it my_array, now points to some place in our computer's memory, and that's where the operating system will physically store our data (ints, longs, whatever).

But how do we refer to an individual items in array?Permalink

As we know, we use indexes. But what is an index really?

To get to some item in our array, we need to know its address in memory.

How do we get it? We have the address of the beginning of the array in our pointer - variable my_array.

We can use the address of the array to get what is at the beginning of the array. So the address of the first item in the array is the same as the address of the whole array. Good.

How do we get the address of the second item in the array?

That's where information about what kind of item we store in the array is now helpful again.

If we store int values, we know that one int has 2 bytes. So the address of the 2nd item in our array is the address of the array + 2 bytes.

Where is 3rd item? Address of our array + 2 times 2 bytes.

See the pattern here?

  • 1st item: the address of array + 0* size of the item.
  • 2nd item: the address of array + 1 * size of the item.
  • 3rd item: the address of array + 2 * size of the item.

So index actually means offset.

It tells us what is the offset from the beginning of the array.

If our pointer my_array would point to a memory address 100, then:

  • The address of the 1st item is 100 (same as the array's address, they're at the same place): 100 + 0 time 2 bytes.
  • The address of the 2nd item is 102: 100 + 1 time 2 bytes.
  • The address of the 3rd item is 104: 100 + 2 times 2 bytes.

See the picture for a visual explanation. Array of 2 ints in memory

You might also like

Join the newsletter

Subscribe to get new articles about Python, code and programming into your inbox!