(no title)
lvkv | 2 years ago
a[index]
as syntactic sugar for the pointer arithmetic: *(a + index)
From this point of view, the existence of a “backwards” index operator makes sense; the arithmetic evaluates to the same address.lvkv | 2 years ago
a[index]
as syntactic sugar for the pointer arithmetic: *(a + index)
From this point of view, the existence of a “backwards” index operator makes sense; the arithmetic evaluates to the same address.
JTyQZSnP3cQGa8B|2 years ago
I have never understood how the compiler is supposed to know that it's (a + 4*index) and not (index + 4*a) which gives different results. Maybe it's based on the type itself and the computation is done by multiplying the "integral" index with it's size, and adding the "pointer" if there is one.
But even this can give wrong values if you're on an embedded platform where you can store your pointers as integers, size_t, or simple defines without a type.
If anyone knows where I can find the explanation in the standard, I've been wondering about it for at least 20 years...
Edit: it seems that a simple example gives the solution: (void*)[void*] is invalid, and int[int] is invalid too, therefore the compiler will get the pointer as the address, and the integral type as the index. I feel stupid for not testing this earlier.
FreeFull|2 years ago
addaon|2 years ago
leni536|2 years ago
mhh__|2 years ago