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

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

    Edit 1 : Decade old question. but still I think this answer will help knowing the compiler's perspective on processing array indexing

    For the compiler they both are the same!

    code 1

    #include
    
    int main()
    {
    
        int myArr[5] = {1, 2, 3, 4, 5};
        int value = myArr[0];
    
    }
    

    code 2

    #include
    
    int main()
    {
    
        int myArr[5] = {1, 2, 3, 4, 5};
        int value = *(myArr + 0);
    
    }
    

    These files if compiled with gcc -S flag, will produce the assembly code file with .s extension I compared both the .s files with kdiff3 and comparison shows they produced the same asm code.

提交回复
热议问题