Why am I being told that an array is a pointer? What is the relationship between arrays and pointers in C++?

后端 未结 6 1045
情书的邮戳
情书的邮戳 2020-12-02 02:39

My background is C++ and I\'m currently about to start developing in C# so am doing some research. However, in the process I came across something that raised a question abo

6条回答
  •  一生所求
    2020-12-02 02:48

    it's as simple as:

    int arr[10];
    
    int* arr_pointer1 = arr;
    int* arr_pointer2 = &arr[0];
    

    so, since arrays are contiguous in memory, writing

    arr[1];
    

    is the same as writing:

    *(arr_pointer+1)
    

    pushing things a bit further, writing:

    arr[2];
    //resolves to
    *(arr+2);
    //note also that this is perfectly valid 
    2[arr];
    //resolves to
    *(2+arr);
    

提交回复
热议问题