Most efficient pointer arithmetic type in c

c, performance, pointer-arithmetic, pointers

Solution

It's unlikely to make any significant difference to performance. In any case, you should always be using the type that's semantically correct, not trying to make premature optimizations for things that aren't going to matter. In the case of indices, `size_t` is the smallest type that's a priori correct, but you may be able to get away with smaller types if you know the array you're working with is bounded. Usually though you should just use `size_t` to be safe.

Problem

I assume that an internal casting happens when we write: `arr[i]` (which is equivalent to `*(arr+i)`). Because `i` can for example be a `short`, `int` or `long` or the unsigned variant of any of these three. So my question is simple: which type should `i` be so that no internal conversion takes place? So that the code can run most efficiently? Crude guess: `size_t`?

Original source

Related problems