|
Posted by Toby Inkster on 01/06/75 11:44
Neredbojias wrote:
> In computer-related languages, the first thing is usually the "0" thing.
> Javascript, for example, numbers the initial item in the images array (-and
> all arrays) "0".
Ususally this is for historical and practical purposes.
An array is often represented in memory as a single number that points to
a location in memory. So the array "myvar[]" might refer to address "1000"
in memory.
If we want to retrieve the first item of the array, we can find it at
memory address 1000. If we want to retrieve the second item in the array,
it's at address 1001; the third item, at 1002; and so on.
myvar[0] stored at 1000+0
myvar[1] stored at 1000+1
myvar[2] stored at 1000+2
etc
So retrieving an array item would be implemented internally by the
programming language like this:
function retrieve_item (array, index)
{
data_width = size_of(data_type_of(array));
address = address_of(array) + (index * data_width);
return retrieve_bytes(index, data_width);
}
Anyway, that's how arrays were implemented in many older programming
languages, and still are in lower-level languages like C. Modern,
higher-level languages don't tend to implement arrays like this, but
retain 0-based indexes because that's what programmers are used to.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
[Back to original message]
|