Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A bit late to this question, nonetheless, this attempts to answer the <strong><em>why</em></strong>-part of your question:</p> <p>Part of the reason is because we use zero-based indexing/offsets when addressing memory. </p> <p>The easiest example is an array. Think of an "array of 6 items" as a location to store 6 data items. If this array's start location is at memory address 100, then data, let's say the 6 characters 'apple\0', are stored like this:</p> <pre><code>memory/ array contains location data 100 -&gt; 'a' 101 -&gt; 'p' 102 -&gt; 'p' 103 -&gt; 'l' 104 -&gt; 'e' 105 -&gt; '\0' </code></pre> <p>So for 6 items, our index goes from 100 to 105. Addresses are generated using <strong><em>base + offset</em></strong>, so the first item is at <em>base memory location</em> 100 + <em>offset</em> 0 (i.e., 100 + 0), the second at 100 + 1, third at 100 + 2, ..., until 100 + 5 is the last location.</p> <p>This is the primary reason we use zero based indexing and leads to language constructs such as <code>for</code> loops in C:</p> <pre><code>for (int i = 0; i &lt; LIMIT; i++) </code></pre> <p>or in Python:</p> <pre><code>for i in range(LIMIT): </code></pre> <p>When you program in a language like C where you deal with pointers more directly, or assembly even more so, this base+offset scheme becomes much more obvious.</p> <p>Because of the above, many language constructs automatically use this range from <em>start</em> to <em>length-1</em>. </p> <p>You might find this article on <a href="http://en.wikipedia.org/wiki/Zero-based_numbering" rel="nofollow noreferrer">Zero-based numbering</a> on Wikipedia interesting, and also <a href="https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm">this question from Software Engineering SE</a>.</p> <p><em>Example</em>:</p> <p>In C for instance if you have an array <code>ar</code> and you subscript it as <code>ar[3]</code> that really is equivalent to taking the (base) address of array <code>ar</code> and adding <code>3</code> to it => <code>*(ar+3)</code> which can lead to code like this printing the contents of an array, showing the simple base+offset approach:</p> <pre><code>for(i = 0; i &lt; 5; i++) printf("%c\n", *(ar + i)); </code></pre> <p>really equivalent to</p> <pre><code>for(i = 0; i &lt; 5; i++) printf("%c\n", ar[i]); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload