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

后端 未结 8 2033
日久生厌
日久生厌 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:38

    There is no difference between

    *(array+10); //and
    array[10];
    

    but guess what? since + is commutative

     *(10 + array); //is all the same
     10[array]; //! it's true try it !
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题