3 ms·
That explanation only partially works. a[5] evaluates to something like: *(a + (sizeof(a) * 5)) so shouldn't 5[a] evaluate to *(5 + (sizeof(5) *
by pyre 15y ago
That explanation only partially works.
a[5]
evaluates to something like:
*(a + (sizeof(a) * 5))
so shouldn't
5[a]
evaluate to
*(5 + (sizeof(5) * a))
Those don't seem equivalent, unless I'm missing something.
- jackowayed 15y agoIt works if you think of it as expanding to *(a + 5). Pointer addition is defined to be scaled by the size of the pointer.
- deleted 15y ago[deleted]
- deleted 15y ago[deleted]
- lurker19 15y agoThat behavior of "+" is defined in the standard, so you don't have to worry about which conforming compiler you are using. Adding integers to pointers can't be a syntax error, since otherwise arrays would be impossible. The commutativity of the bracket operator is a silly side effect of the commutativity of addition, but not broken in any way except maybe readability.
- lurker19 15y agoIt is also interesting to note that this behavior of '+' in C an C++ and Java is basically (logically) multiple-dispatch on the types of its arguments, but the language does not allow programmers to define operators like this -- C does not allow user-defined operators at all, in C++ you have to define the operator in every class that can appear on the left-hand-side, in Java you can define a method which looks almost like an operator but still has to be smeared across all classes (unless you write a utility class method Foo.plus(A, B) that eliminates the asymmetry between the two operands). This subordination of functions below objects, and special reservation of rights for builtins, is what functional programming language uses loath about non-FP languages.
- lurker19 15y agoAs noted in the SO comments: when compiling the "+", the compiler uses the sizeof of the referenced type of whichever addend is actually a pointer.
- pyre 15y agoI think that I've been away from statically-typed languages for too long. I was just thinking of the pointer as an int whose value just happens to be a memory address and forgetting that the compiler knows it's a pointer.
- lurker19 15y agoTo first order approximation, C is the language with no magic. That's its most beloved quality. That gets you really far, until you get creamed by all the subtle magic in pointers and arrays.
- lurker19 15y agoThis works just as well in dynamically typed language by using your language's version of .getClass() or instanceof. It would just happen at runtime instead of compile time. It only wouldn't work in very weakly typed languages like ... assembler?
- pyre 15y agoI was evaluating 5 + a to mean int + int, because the pointer is stored in an int. Maybe I should have just stated, "It's been awhile since I've work with pointers." Most dynamic languages don't have a concept of pointers, or at least not pointer math.
- barrkel 15y agoNo; a[5] evaluates to (a + 5), and 5[a] as (5 + a). Separately: e + p where e : integer-type and p : pointer-type p + e where e : integer-type and p : pointer-type are evaluated roughly as: (pointer-type)(e*sizeof(*p) + (size_t)p) (pointer-type)((size_t)p + e*sizeof(*p)) but these are very rough translations, because we're wandering into implementation-defined territory (substitute uintptr_t for size_t as desired etc). The type analysis on the addition is reminiscent of e.g. promoting integers to floats: i + f where i : integer-type and f : float-type f + i where i : integer-type and f : float-type correspondingly: (float-type)i float-type::+ f f float-type::+ (float-type)i The '+' built-in is overloaded in C.