Re: 0-based Indexing

new topic     » goto parent     » topic index » view thread      » older message » newer message

I wonder how many ppl will balk at the explanation given below ;]
jbrown

On Wed, Jul 16, 2003 at 04:04:08PM -0500, C. K. Lester wrote:
> 
> 
> I don't get it. Why do some languages use 0-based indexing? It's got to be
> the dumbest thing in programming language history. Or maybe not.
> 

Its because of the fact that (*cough* normal) computers count starting at
zero.

It is also related to the way C does things ... for an array void_pointer,
void_pointer[i] is the same as void_pointer+(sizeof(*void_pointer)*i)...
to be a bit clearer, void_pointer[i] would be the same as taking the number of
bytes that one element of void_pointer would take up, and then multipling that
by i, which would give the number of bytes that i elements would take up (this
number is needed to determine where the element is located relative to the
start of the array).

So, if void_pointer is an array of int's, i is 5, and sizeof(int) is 4, then
the 6th element (remember, we start counting at zero here) is sizeof(int)*i
or 4*5 or 20 bytes away from the begining of the array.

The final step is to add the value held in void_pointer to what we got above,
this gives us an absolute memory address which we can then access like a normal
variable. (Remember that in C an array is merely a constant pointer.)

Using this scheme, void_pointer[0] = void_pointer+(4*0) = void_pointer,
which makes the address held in void_pointer to be the very first
element in our array. (This is probably outdated by languages that use more
advanced techniques to store its data.)

I have heard that the technical reason that sequences in Euphoria start at
1 is because in the C array that is used internally, element 0 is used to hold
the length of the sequence. Not sure if this is true or not.

jbrown

> 
> 
> TOPICA - Start your own email discussion group. FREE!
> 
> 

-- 
 /"\  ASCII ribbon              | http://www.geocities.com/jbrown1050/
 \ /  campain against           | Linux User:190064
  X   HTML in e-mail and        | Linux Machine:84163
 /*\  news, and unneeded MIME   | http://verify.stanford.edu/evote.html

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu