5 ms·
I grumbled about indexing starting at 1, but once you get used to it it makes a lot of problems easier. I've spent the last twenty-ish years in C and string ma
by fullstop 5y ago
I grumbled about indexing starting at 1, but once you get used to it it makes a lot of problems easier.
I've spent the last twenty-ish years in C and string manipulation just sucks. Think about it, you declare a buffer of length 20, the indexes are from 0->19, and the 19th byte needs to be a null if you are using it as a string and are using the entire buffer. Also, the standard library is not guaranteed to null terminate in all situations.
Lua's string indexing feels far more natural to me.
- tannhaeuser 5y agoThis. The insistence on using 0-based string offsets is purely a C thing (where it makes sense) inherited on to languages that wanted to stay close to C or appeal to C devs (even though it does not make sense). An easy way to check is looking into awk which, as a DSL for string manipulation written itself in C, deliberately uses 1-based string offsets, and where many/most common string expressions collapse to a very compact form, which makes even more sense because empty string results are interpreted as false in conditions.
- jhvkjhk 5y agoIt's not a C thing, it's a math/utility thing. Dijkstra: Why numbering should start at zero https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- corysama 5y agoWhich is funny because Lua got it’s 1-base from FORTRAN which, I believe, adopted it to make TRANslating math FORmulas easier.
- tovej 5y agoI agree in part that it's a C thing. More accurately it's an offset from the array base thing. The first element is at index 0 because its address is base + 0 * sizeof(element) The second element is at index 1 because its address is base + 1 * sizeof(element)
- vlovich123 5y agoSmall correction. The 20th byte (at index 19) needs to be null. The mismatch with the English language and how people naturally count is definitely there and annoying. And yes, string manipulation in C is especially broken although I think the indexing is the smallest problem there. However, it’s extremely natural when you think about it in terms of memory access. For example, in a 1-based indexing system, ptr[0] would point 1 character behind your pointer (weird) and ptr[-1] would point 2 back (wtf). Having the index map neatly to the offset makes a lot of sense to me. In fact, when I first started programming in VB6 20 years ago and only had a math background, the 1-based indexing was natural but I could never figure out why I had so many bugs related to array and string offsets. I’ll also note that most programming languages are 0-based and interop with C is not really the goal (Java, JavaScript, Ruby, Python, etc). In fact, Python and Perl’s string manipulation is some of the best out there and they are 0 indexed.
- fullstop 5y agoI figured that someone would pipe up about the 19th byte vs 19th index, and I'm glad. This is just semantics, but does index 0 represent the first byte or the zeroth byte? I completely agree regarding memory access, but would argue that strings and memory should not be treated in the same way. Having the index represent the length - 1 has caused countless off-by-one bugs [1] that would not have been there in the first place if string indexes started with 1. Java, JavaScript, Python, (maybe Ruby, I'm not fluent there) also bite the user if you attempt index data outside of the string's range. C will happily index whatever you want, and these bugs can often remain hidden for decades. 1. https://cwe.mitre.org/data/definitions/193.html https://cwe.mitre.org/data/definitions/193.html
- Joker_vD 5y agoIndex N represents "skip N elements". That gives you very easy additive/subtractive behaviour, unlike "how many numbers are there from 7 to 17?" scenarios. Sure, 17-7 gives you 10, but that's not the final answer, you have to add 1 to get the right answer, 11. Sorry, no, you actually subtract 1 and the right answer is 9. Wait, no: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, okay, 11 in total, so you have to add 1, got it right the first time.
- sporedro 5y agoThe indexing starting at 1 is something that has always annoyed me. I just haven't seen any other language make that decision, and I'm not sure the reasoning for it really outweighs the fact every other language just goes with 0 due to the origin of it. Lua is a great language for sure though.
- fullstop 5y agoPascal did, but I believe that was because index 0 contained an 8-bit length. There was no need for null termination, and strings were limited to 255 bytes.
- mattkrause 5y agoFortran, Matlab, and Julia all start arrays at 1, either always (Matlab) or by default (Fortran, Julia).
- toast0 5y agoErlang indexes starting at one, except when it doesn't. Tuples are 1 indexed, lists are 1 indexed (in the standard library, anyway), but binaries are zero indexed.