3 ms·
Small 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 a
by vlovich123 5y ago
Small 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.
- quietbritishjim 5y agoAnother way to look at it, in languages like Python that support slicing, is that the indices refer to the boundaries between the elements rather than the elements themselves. a b c d 0 1 2 3 4 From this 4 character string you can slice s[1:3] and get 'b', 'c'. s[i:j] will always have j - i characters (ignoring negative indices). s[i] is as if it were short for s[i:i+1] e.g. s[3] is s[3:4] and that gives you 'd'. Admittedly C++ doesn't have slicing so this is less relevant, but I think it's still an interesting aspect to the discussion (without getting bogged down in ideology).
- germandiago 5y agoIt has with std::span