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

前端 未结 4 1174
攒了一身酷
攒了一身酷 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 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<

    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.

提交回复
热议问题