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

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

    There is no functional difference. The decision to use either form is usually made depending on the context in which you are using it. Now in this example, the array form is simpler to use and read and hence is the obvious choice. However, suppose you were processing a character array, say, consuming the words in a sentence. Given a pointer to the array you might find it easier to use the second form as in the code snippet below:

    int parse_line(char* line) 
    {
        char* p = line;
        while(*p)
        {
             // consume
             p++;
        }
        ...
    }
    

提交回复
热议问题