0 reply content: two links are provided:
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:
- 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.
- 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.
- 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.