Why is the same value output for A[0], &A, and *A?

前端 未结 4 1157
攒了一身酷
攒了一身酷 2020-12-07 00:21

I am doing a little experiment.

#include
#include
using namespace std;
int main()
{

  int A[5][5];
  cout<

        
相关标签:
4条回答
  • 2020-12-07 00:45

    A[0] This equivalent to *(A + 0), or more simply *A.

    &A is a little more tricky. A is of type int[5][5], which is represented by a continous region of 100 bytes on the stack. The address of A is the start of that region - which is equal to the pointer to the first element. That first elements adress is also the storage location of *A.

    0 讨论(0)
  • 2020-12-07 00:51

    An array, at its most basic level, is a pointer to a spot in memory. The other elements in the array are stored consecutively after that element and the index tells the computer how many places to jump from the first element to get to the desired one. A[0] is printing out the address of the first element in the first row, &A is printing the address that A is located at, which is where the first element of the first row is, and *A is the same as A[0].

    0 讨论(0)
  • 2020-12-07 01:03

    The first thing to understand is what you are printing:

     cout<<A[0]<<"  "<<&A<<"   "<<*A;
    

    The expression A[0] an lvalue expression with type int[5] referring to the first internal array inside A, &A is an rvalue-expression of type int (*)[5][5] that points to the array A. Finally *A is equivalent to A[0], that is an lvalue expression of type int[5].

    There are no operators defined in the language (nor can you provide them) that will dump either a int[5] or a int (*)[5][5], so the compiler tries to find the best match it can and finds that there is an operator that prints a void*. int[5] can decay into a int* that refers to A[0][0], and that is itself convertible to a void*. int (*)[5][5] is a pointer and thus convertible to void*, so that overload is valid for both cases.

    The language defines the layout of an array in memory, and in particular it requires that the array and the first element of the array are laid out in the same memory address, so if you were to print the addresses of &A and &A[0] it would print the same value, and because &A[0] is also in the same memory location of the first of its elements, &A[0][0] also refers to the same address.

    Going back to the code above what you are printing is:

    cout<<         static_cast<void*>(&A[0][0]) 
        << "  " << static_cast<void*>(&A)
        << "  " << static_cast<void*>(&A[0][0]);
    

    which following the reasoning above must have the same exact value, even if the type is not the same in the second case.

    0 讨论(0)
  • 2020-12-07 01:07

    A[0], &A, *A all are pointers of distinct types that all point to the same memory location. Same value (sort of), different types.

    Expression    Symmetric                                          Type  
    ----------------------------------------------------------------------------
    A           address of first row.                               int[5][5]
    &A[0][0]    address of first element                            int* 
    &A          address of 2d array                                 int(*)[5][5]   
    *A         = *( A + 0) = A[0] = address of first element        int[5] = decays to int*
                                                                             in a expression
    

    My example of 5*4 dimension char arrays:

                          A
                        +---201---202---203---204---206--+
        201             | +-----+-----+-----+-----+-----+|   
        A[0] = *(A + 0)--►| 'f' | 'o' | 'r' | 'g' | 's' ||
        207             | +-----+-----+-----+-----+-----+|
        A[1] = *(A + 1)--►| 'd' | 'o' | '\0'| '\0'| '\0'||
        213             | +-----+-----+-----+-----+-----+|
        A[2] = *(A + 2)--►| 'n' | 'o' | 't' | '\0'| '\0'||
        219             | +-----+-----+-----+-----+-----+|
        A[3] = *(A + 3)--►| 'd' | 'i' | 'e' | '\0'| '\0'||
                        | +-----+-----+-----+-----+-----+|
                        +--------------------------------+
    

    A brief explanation about figure example.

    • In figure A represents complete 2-D array starts with address 201, and
    • &A gives address of complete 2-D array = 201
    • *A = *(A + 0) = A[0] points to first row = 201
    • Note value A[0][0] is 'f' in my example, and &A[0][0] gives address of [0][0] element = 201
    • Note &A[0][0] is same as *A, because &A[0][0] => &(*(*A)) => &**A => *A

    So all A[0], &A, *A, A are same but symmetrically different.

    To observe difference among the A[0], &A, *A, A. Type to print sizeof() information. e.g.

     cout<<sizeof(A[0]) <<" "<<sizeof(&A) <<" "<<sizeof(*A) <<" "<< sizeof(A); 
    

    Second try to print next location address using:

     cout<<(A[0] + 1)<<" "<<(&A + 1) <<" "<<(*A + 1)<<" "<<(A + 1);
    

    For more detailed explanation, must read this answer.

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