Re:Again on 0-based vs. 1-based indexing
by lrossi on Wednesday January 20, 2021 @08:00AM
(#25845890)
Another language trivia: for strings, Pascal (which has 1-based indexing) does actually use the character at index 0 to store the string length behind the scenes. Because of this, indices are converted into offsets directly, without having to subtract one. It’s also the reason why strings were limited to 255 characters.
[ Parent
| on HN ]
Re:Again on 0-based vs. 1-based indexing
by DonaldFisk on Wednesday January 20, 2021 @09:59AM
(#25847152)
If Pascal had 1-based indexing, it wouldn't be possible to store the string length at index 0. The first character in a string is always at index 1 because of this. Strings are
Packed Array[0..255] of Char
See https://www.pascal-programming.info/lesson10.php
Re:Again on 0-based vs. 1-based indexing
by clouddrover on Wednesday January 20, 2021 @11:28AM
(#25848338)
Pascal has both. Static arrays in Pascal can have arbitrary indexes with mixed data types:
http://docwiki.embarcadero.com/RADStudio/Sydney/en/Structure...
Examples:
- var MyArray: array [1..100] of Char;
- packed array[Boolean, 1..10, TShoeSize] of Integer;
Dynamic arrays are indexed from zero.