Difference Between *(Pointer + Index) and Pointer[]

后端 未结 8 2189
日久生厌
日久生厌 2020-11-27 03:56
int* myPointer = new int[100];

// ...

int firstValue = *(myPointer + 0);
int secondValue = myPointer[1];

Is there any functional difference betwe

8条回答
  •  庸人自扰
    2020-11-27 04:42

    No, they are functionally equivalent.

    First, index is scaled up to the type size then added to the myPointer base, then the value is extracted from that memory location.

    The "better practice" is the more readable one, which is usually, but not necessarily always, the myPointer[index] variant.

    That's because you're usually interested in an element of the array, not the memory location to dereference.

提交回复
热议问题