Why do arrays in most programming languages count from 0, which is better than counting from 1?

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: provide two links:
1. Why does the indexing start with zero in 'c '? Http://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c )
2. Why numbering shoshould start at zero, by Dijkstra. (http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF ) Not all, Pascal can not count from 0, for example, from-100 .. 100 consider the C language pointer
Int a [10];
Then a [0] === * (a + 0)
I think this is the case. let's first explain why the C language array starts from 0.
As we all know, arrays in C language are directly used to operate memory, we must start with memory addressing:
Take the allocation of "active behavior order" as an example: set the base address of the array to LOC (a c1 c2), and each array element occupies 1 address unit, then, the physical address of aij can be used in the first-line addressing function compute:
LOC (aij) = LOC (a c1 c2) + (I-c1) * (d2-c2 + 1) + (j-c2) * l
To A general three-dimensional array: A [c1.. d1] [c2.. d2] [c3.. d3], the physical address of aijk is:
LOC (I, j, k) = LOC (a c1 c2 c3) + (I-c1) * (d2-c2 + 1) * (d3-c3 + 1) + (j-c2) * (d3-c3 + 1) + (k-c3) * l
Obviously, if c1 c2 c3 is 0, it will greatly simplify the computing !! The more multidimensional the array is, the more obvious the effect is.
This is obviously a big benefit for computer addressing computing.
This is why the earliest C language array starts with 0
What other reasons do you think they are beautiful?
In fact, when computing is no longer a bottleneck, the subscript of the array also starts from 1.
For example, matlab
Isn't matlab beautiful? = technically, the subscript of the array is the memory offset. For example, if the p pointer points to the memory of the array, p + 0 is the memory address of the first element, and p + n × sizeOfElement is the memory address of the nth element. when I learned the C language, I understood it as follows:
Array name a is the memory address of the first element of the array.
Retrieving array elements is the process of accessing the memory through Address.
Take 1st elements, that is, * (a + 0)
Take 2nd elements, that is, * (a + 1 ),
...
So the subscript may not start from 0. arrays in ObjectPascal can start from 1. this "starting from scratch" problem, I personally think this is a historical issue, just like the letter distribution of the current computer keyboard, there is no special theoretical basis for many language arrays NoI started from 0 (such as MATLAB), but a computer science pioneer Edsger Dijkstra (the one who invented Dijkstra's most short-circuit algorithm) wrote an article in 1982, advocating subscript starting from 0, it is called "Why we have to count from 0 ".

Full text here: Why numbering shoshould start at zero (EWD 831)

Summary:
  1. Describe the natural number subsequence. The difference between the upper bound and the next subsequence is the length of the subsequence. the array subscript can be understood as a special seed sequence.
  2. The lower bound should contain the elements in the data, and the previous session should not contain the elements. In other words, the lower bound should be the first index of the array. Otherwise, we may use the real number of non-natural numbers as the lower limit for some subsequences.
  3. If conditions (1) and conditions (2) are considered, there are two ways to describe the upper and lower bounds: 1 <= I <N + 1 or 0 <= I <N. obviously N + 1 is too ugly as the upper limit, so we should start the subscript from 0 ..
Since Quora Travis Addair's answer to Why do array indices start with 0 (zero) in programming languages?
However, it seems a little difficult to translate. The description interval (a set of real numbers determined based on the maximum and minimum values) and the array elements in the description language are two different things... It is more reliable to explain from the implementation of C language. Counting on the hardware starts from all the bits being low (positive logic), that is, 0 in the unsigned integer. 0 <= a mod B <= B-1
0 is the pixel of the + operation, and 1 is the pixel of the * operation.
* More than +, so 0 is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.