Re: 0-based Indexing

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

On 16 Jul 2003, at 17:26, jbrown105 at speedymail.org wrote:

> 
> 
> 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.

BASIC and Pascal used the first byte as length. (It feels odd that RobC 
would use an example from Basic, and still not implement CASE and 
GOTO.) So the string storage data did actually start at zero offset, but not 
the string itself. Naturally my gripe with Turbo Pascal is that it began a move 
to C strings, so one could not store a null in byte in the string. But then, 
DOS frowned on that too.

Kat

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

Search



Quick Links

User menu

Not signed in.

Misc Menu