Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index vs. Pointer

Tags:

I'm using arrays of elements, many of which referencing each other, and I assumed in that case it's more efficient to use pointers. But in some cases I need to know the index of an element I have the pointer to. For example I have p = &a[i] and I need to know the value of i. As I understand it, i can be computed through p - a. But this operation inherently involves division, which is expensive, whereas computing an address from an array index involves a multiplication and is faster.

So my question is, is cross referencing with pointers in a case where you need the indexes as well even worth it?