Why are the arrays in most programming languages counted from 0, and what are the benefits of counting starting from 1?

Source: Internet
Author: User

Reply content:

provides two links:
1. Why does the indexing start with zero in ' C '? ( / http stackoverflow.com/quest ions/7320686/why-does-the-indexing-start-with-zero-in-c )
2. Why numbering should start at zero, by Dijkstra. ( http://www. cs.utexas.edu/users/ewd /ewd08xx/ewd831. PDF Not all, the Pascal language can be counted not from 0, such as from-100. 100 consider C-language pointers
int a[10];
Then a[0] = = = (a+0)
I think that's the first thing to say about why the C array is starting from 0
As we all know, the C language array is the direct manipulation of memory, then we must start from the memory addressing:
Take the assignment of the main sequence of behavior as an example: the base address of the array is loc (a C1 C2), and each array element occupies an L address cell, then the physical addresses of the AIJ can be computed using the linear addressing function:
LOC (AIJ) =loc (a C1 C2) + ((I-C1) * (D2-C2 + 1) + (J-C2)) *l
Generalized to a general three-dimensional array: A[c1. D1] [C2. D2] [C3. D3], the physical address of the 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, here the C1 C2 C3 for 0 will greatly simplify the calculation there are wood!! The more multidimensional the array effect the more obvious
This is obviously a big benefit for computer addressing calculations.
That's why the first C-language array starts with 0.
As for what other reasons feel beautiful ah what is later people yy
In fact, at a time when computing is no longer a bottleneck, array subscripts also start at 1.
such as Matlab
is matlab not beautiful? = = from the technical realization, the subscript of the array is the offset of the memory. such as the P pointer to the memory of the array, p+0 is the memory address of the first element, P+nxsizeofelement is the memory address of the nth element of the C language when this understanding:
The array name A is the memory address of the first element of the array
and the array element is the process of accessing the memory through the a address.
Take the 1th element, that is * (a+0)
Take the 2nd element, which is * (a+1),
...
So the subscript starting from 0 not necessarily, objectpascal in the array can start from 1, the "zero-start" problem, I personally think this is an artificial historical legacy, like the current computer keyboard distribution of letters, there is no special theoretical basis for many language arrays is notStarting from 0 (such as MATLAB), but a computer science pioneer Edsger Dijkstra (the one who invented Dijkstra's shortest-path algorithm) wrote in 1982, advocating starting from 0 to do the subscript, called "Why start from 0 to count."

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

To summarize:
    1. Describe the natural number sub-sequence, the upper and lower bounds should be the length of the subsequence, array subscript can be understood as a special sequence of this seed.
    2. The nether should contain elements within the data, which should not be included in the previous session. In other words, the nether should be the first index of the array. Otherwise, for some sub-sequences, we have to use the real numbers of unnatural numbers as the nether.
    3. If the condition (1) and the condition (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 as the upper bound is too ugly, so we should let subscript starting from 0.
Translated from Quora Travis Addair ' s answer to what do array indices start with 0 (zero) in many programming languages?
But it feels a little bit wrong to finish translating. It is not a different thing to describe the interval (the set of real numbers judged by maximum and minimum values) and the array elements in the description program language ... or from the implementation of C language to explain the more reliable. What counts on hardware is all starting with all bits being low (positive logic), which is 0 inside the unsigned integer. 0 <= a mod b <= b-1
0 is the element of the + operation, and 1 is the unitary of the operation.
* More than + use, so with 0.
  • Related Article

    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.