3 ms·
The alignment doesn't matter. `index` is not an offset in bytes, it's just an index, a number. The position in memory of the variable also doesn't matter as it
by cosarara 7y ago
The alignment doesn't matter. `index` is not an offset in bytes, it's just an index, a number. The position in memory of the variable also doesn't matter as it is not used, only the value.
`index[arr]` means `*(index + arr)`.
Which is exactly the same thing as arr[index], no matter how you look at it. The type of arr is taken into account for pointer arithmetic.
Whatever works with `arr[index]` will also work with `index[arr]`.
You can also use a constant, for example `2[arr]`.
Damn I hate HN formatting.
- sifar 7y agoyes, I realized this after asking the question :).I must admit I never thought it in this way. Thinking more about this, this might fail if the compiler uses post-incrementing load/stores which update the pointer addresses too. Or will it not use them if I am using index[arr] ? Lets say the load/store updates the pointer too. So after first execution, ptr will index+arr in both cases, but during the next iteration , the former will be ptr[index] while the latter will be ptr[arr].