6 ms·
The a[b] implemented as *(a+b) Thing, is how we were taught to think about array indexing in the CS lectures of the 70s
by ggm 6y ago
The
a[b] implemented as *(a+b)
Thing, is how we were taught to think about array indexing in the CS lectures of the 70s
- sgtnoodle 6y agoAre those not equivalent expressions in modern C? I imagine there are more optimal and less optimal ways of actually doing the indexing in machine code and the former may be better semantics, but I would think a compiler would generate identical machine code for both.
- tinus_hn 6y agoI’m pretty sure you have to take the size of the objects in mind.
- throwaway525142 6y agoNo, these are equivalent. The size of the objects is implicitly taken into account by the compiler, it knows the size of the objects by the type of the pointer.
- amelius 6y ago"Pointer arithmetic" takes care of that. Adding an integer to a pointer will multiply the size of the type pointed to by the integer and adds that to the pointer.
- dvko 6y agoIt’s also in the K&R book IIRC, stating that the two are equivalent.
- st_goliath 6y agoAnd that's how it's still taught nowadays. Both the C89 and the C99 standard draft contain the following: > The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))) In fact the expressions a[b] *(a + b) and b[a] are equivalent. Here is a perfectly valid snippet of C code that will print out 't': putchar(3["test"]);
- mschuster91 6y agoI understand why that example works but I struggle finding a valid use-case, aside from code golfing...
- cat199 6y agospeculating, but don't think it's about use case so much as it is about it being a simple way to implement ('C is portable assembly') which probably carried through to our more current notion of this being a 'language level' thing
- charcircuit 6y agoNot everything needs a valid use case. It can exist just for fun.
- mschuster91 6y agoThe thing is, the more complex a spec is (or rather, how much stuff it allows that will never be used), the bigger the danger is that somewhen down the line, this will introduce a security or other issue.
- Koshkin 6y agoI think the assumption that addition is commutative in any context simplifies the spec rather than makes it more complex.
- mschuster91 6y agoIt however is not "natural" for someone who doesn't know the obscure bits of history in a standard written many decades ago. Someone writing, say, a static code analysis tool or an IDE may not assume that it is possible that in the expression `a[b]` a may be something else than a pointer / array.
- 6y ago
- raverbashing 6y agoHence why it "can be written" as b[a] as well Edit: it doesn't blow up, not even with -Wall and -std=c99
- stephencanon 6y agoIt will not “blow up” in modern compilers, nor can it, because that’s _how the operator is defined_.
- st_goliath 6y ago> (yes it will probably blow up in modern compilers, or at least give you a warning) Nope. For the code snippet I posted an hour ago, even with -pedantic -Wall -Wextra gcc won't issue any warnings. And why should it? It's perfectly standards conformant, because the standard actually defines the [] operator through the equivalent addition expression.
- MaxBarraclough 6y ago> why should it? It's extremely poor style, even if the behaviour is identical.
- st_goliath 6y agoSo you expect to compiler to give you style points for your code? To be clear: it isn't just coincidentally identical behaviour, it is defined by the standard to be equivalent.
- rbanffy 6y agoIt should at least emit a humorous message asking the developer what are they really trying to accomplish by that.
- MaxBarraclough 6y agoYes, compilers should warn on indisputably poor style, even when program behaviour might still be correct. This is helpful to the programmer, who probably didn't intend to write their code that way. Fortunately compilers already do this. GCC will warn you about unused variables, for instance.
- nspain 6y agoThat's how we were taught a few years ago too! It really helped it "click" that array elements are stored contiguously.