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

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

    Actually , When an Array 'a' is initialized a pointer to its first memory location ie.. a[0] is returned which is nothing but a ;

    So if you do 'a+1' it is actually a pointer to a[1]

    if you do 'a+2' it is actually a pointer to a[2]

    if you do 'a+3' it is actually a pointer to a[3] so on ,

    so if you do *(a+1) you will get value of a[1] and similar for other values also. if you do *(a) you actually get a[0], So i think its pretty clear now how it works..

    0 讨论(0)
  • 2020-11-27 04:19

    Functionally, they are identical.

    Semantically, the pointer dereference says "Here's a thing, but I really care about the thing X spaces over", while the array access says "Here's a bunch of things, I care about the Xth one."

    In most cases, I would prefer the array form.

    0 讨论(0)
  • 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<stdio.h>
    
    int main()
    {
    
        int myArr[5] = {1, 2, 3, 4, 5};
        int value = myArr[0];
    
    }
    

    code 2

    #include<stdio.h>
    
    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.

    0 讨论(0)
  • 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++;
        }
        ...
    }
    
    0 讨论(0)
  • 2020-11-27 04:27

    More readable and more maintainable code is better code.

    As for functional part... There is no difference. Both times you are "playing with memory".

    0 讨论(0)
  • 2020-11-27 04:29

    There is no functional difference I know of but the form myPointer[1] is ultimately more readable and far less likely to incur coding errors.

    DC

    The form *(myPointer + 1) does not allow for changing the type of pointer to an object and therefore getting access to the overloaded [] operator.

    Also debugging is far harder

     int *ints[10];
     int myint = ints[10]; 
    

    is easier to pickup visually than

     int *ints;
     int myint = *(ints + 10); 
    

    also the compiler can insert range checking to catch the error at compile time.

    DC

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