Why Arrays start at 0 and not 1

Asked

Viewed 1,684 times

13

Why arrays start at 0 and not 1 ?

There is no 0 people, 0 animals, 0 nothing...(do not cling to this part)

What is the point of doing this? I believe that it should not be just me who does not understand the logic of this, if there is one.

  • Because what starts at zero is the indices, the size of the array is still countable in the same way.

  • Because 0 comes before 1

  • The question has already been answered, and very well indeed. But I leave one aside, when you refer Não existe 0 pessoas, 0 animais, 0 nada. you are quantify something, when in fact what we want here is to indicate the position of something in an array.

  • (não se apeguem a esta parte) oopps

Show 1 more comment

3 answers

19


An array is a set of elements arranged side-by-side in memory, from an address.

For example, let’s say in memory you have 10 integers in an array. And that the address of that array in memory is 100 (the number chosen by the OS, which found a free memory area for the array).

As each integer has 4 bytes, the first element will be at index 100 itself, the second at index 104, the third at index 108, and so on.

How do you calculate the memory position of the element in general? See:

Primeiro elemento: 0 * 4 bytes + 100 bytes
Segundo elemento: 1 * 4 bytes + 100 bytes
Terceiro elemento: 2 * 4 bytes + 100 bytes.

Looking at the calculations, it is easy to see why it was agreed that the index would be based on 0, not on 1. Thus, it would not be necessary to subtract 1 if such a calculation had to be made (and in the early languages, it was very common for calculations to be made like this, because a lot of memory was manipulated in the hand). And even for a compiler, it would be easier to do the conversion.

Another reason is that in mathematics (and the first applications were very mathematical) it is very common to use elements based on 0.

Note that this is only a convention. There is no technical reason in terms of performance or memory expenditure that justifies it. Some older compilers had indexes based on 1, or even configurable (as is the case with Pascal, language prior to C). There is no bit or performance loss as the compiler is able to subtract this index at compile time, but using a 0-based index was very practical and this convention remains to this day.

  • Good answer. You would have some, good, sources/references for me to study further in depth?

  • 1

    Valdeir I read this Book and recommend if possible that you read.

  • Heritage of C, too. There are several reasons for this, including getting closer to hardware (don’t forget, C is structured Assembly)

1

This answer should be a comment. An English answer to your question is at: https://en.wikipedia.org/wiki/Zero-based_numbering

My comment is that you are confusing the ordinal number zero (in linguistics) with the cardinal number zero. The index of an array does not designate a quantity (not a cardinal number), but a position in a sequence. In everyday life, it is more common to index an ordering beginning with the 'first' element, followed by the 'second' and so on. But since both cardinal numbers and ordinals generalize the natural numbers, both include zero, it being perfectly possible to index an ordination starting with "zero-nth" element, the lowest position in the sequence.

  • This "comment" seemed to me quite relevant as "answer". And thank you very much for remembering the difference between ordinals and cardinals, I myself live by exchanging both when I’m not actively thinking about the subject

-4

Simply because the decimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9, not 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 as we commonly count (erroneously).

  • So what would you say in this case: lista[-1]

  • Computer uses binary digits, so it should only be 0 and 1? Maybe you’re referring to the natural numbers, which may or may not start at 0, depends a lot on the axiomatic system you’re using

Browser other questions tagged

You are not signed in. Login or sign up in order to post.