6 ms·
All craziness but then: >a[b] is literally equivalent to *(a + b). Is this obscure? I thought that's pretty much the first thing you learn about arrays in C?
by nothis 7y ago
All craziness but then:
>a[b] is literally equivalent to *(a + b).
Is this obscure? I thought that's pretty much the first thing you learn about arrays in C? It's pointers, all the way down.
- saagarjha 7y agoIt's syntactic sugar that's more poorly abstracted than most people realize.
- arcticbull 7y agoAgreed, and I never made the connection that array[index] could be written as index[array] haha.
- anticensor 7y agoOnly if index and array have the types of same size. Because pointer arithmetic works in terms of object sizes, not in individual bytes.
- erik_seaberg 7y agofoo[3] *(foo + 3) *(3 + foo) 3[foo] They all do the same thing.
- arcticbull 7y agoInteresting! I assume because the literal is being coerced to be the size of the variable? After all: foo[3] == (void *)((usize)foo + (usize)(3 * sizeof(*foo)))
- erik_seaberg 7y agoYeah, that's how pointer arithmetic works. Adding an int and a pointer assumes an array and gives you a pointer to the nth element in that direction (which had better exist, for your sake). char pointers can be used for byte offsets if a byte is a char on your platform (and it's been quite a while since that wasn't true). You can also subtract two pointers into the same array and get the distance (in elements, not bytes). a[b - a] == b.
- harry8 7y agotypedef struct thing { char a; long long int nothing; } thing_t; #define P(x) printf("%s %c\n", #x, x) int main(int argc, char **argv) { thing_t array[1024]; array[8].a = 'H'; P(array[8].a); P((8[array]).a); P((*(8 + array)).a); }
- bingerman 7y agoAs the author continues, it becomes mildly weird only when you realize that you can write b[a] and it just works (tm). I've seen students saying the compiler somehow checks that the "a" is arrayish so the swapped version doesn't make sense.
- atq2119 7y agoNot really. The way it's usually introduced is that you get the same "reference" both ways. The fact that it's literally equivalent, and especially that there's no pointer type requirement on the left-hand-side, with the consequence of allowing ridiculous code like 2[array], is pretty obscure. Even more so because the equivalence doesn't work that way in C++ -- in general, features of C that aren't available in C++ tend to be not as widely known.
- eMSF 7y ago>Even more so because the equivalence doesn't work that way in C++ What do you mean? As far as I remember, C++ is very similar in this respect when it comes to array and pointer types.
- asveikau 7y agoPerhaps they mean that operator overloading can break that equivalence. Which is true, but I would argue an implementation that makes those operators behave differently is likely ill advised.
- atq2119 7y agoOnly for the most primitive types, though. For anything else, operator[] is used in C++ instead, without an operator+ fallback. So for example, if your array is a std::array instead of a C-style array, saying 1[array] will not work.