4 ms·
Pascal (and Modula if I'm not mistaken) supports this too.
by hans_castorp 1y ago
Pascal (and Modula if I'm not mistaken) supports this too.
- LiamPowell 1y agoAda too, which is of course Pascal-based. Like many programming language features, it feels like it was lost simply because C didn't have it and everyone wanted to copy C.
- pjmlp 1y agoMany BASIC dialects as well. Hence why the whole base index discussion only became relevant in C based languages.
- ubercore 1y agoReminds me of this fantastic talk: https://www.youtube.com/watch?v=wo84LFzx5nI https://www.youtube.com/watch?v=wo84LFzx5nI
- IsTom 1y agoIn C can't you just offset pointer and then you'll be able to index with arbitrary starting value?
- michaelrpeskin 1y agoYes. I do this a lot when writing linear algebra stuff. All the math texts write things in 1-based notation for matrices. The closer I can make the code match the paper I'm implementing makes life so much easier. Of course there's a big comment at the beginning of the function when I modify the pointer to explain why I'm doing it.
- LiamPowell 1y agoTechnically no, a pointer pointing outside of its array (or similar) at any point is undefined behaviour. More importantly for this discussion, without support from the language it's not very ergonomic to work with. What happens when you need to call strlen, memcpy, or free?
- kevin_thibedeau 1y agoIt works in this case where you want to move the zero index forward a few cells to a valid offset. It is only UB for the general case where the offset may land outside valid memory. C has always supported negative indices, so moving index zero forward into the middle of the array is fine.