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

I am doing a little experiment.

#include<cstdio> #include<iostream> using namespace std; int main() { int A[5][5]; cout<<A[0]<<" "<<&A<<" "<<*A; return 0; } 

It prints the same value for all cases. Can anyone explain why this is so?

-one
c ++
source share
4 answers

The first thing to understand is what you type:

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

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

There are no operators in this language that cannot provide them, which will unload either int[5] or int (*)[5][5] , so the compiler tries to find the best match and finds that there is an operator that prints a void* . int[5] can decay to int* , which refers to A[0][0] , and is itself converted to void* . int (*)[5][5] is a pointer and, therefore, is converted to void* , so overload is acceptable for both cases.

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

Returning to the code above, you type:

 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 in the second case does not match.

+6
source share

A[0] , &A , *A are all pointers of different types that point to the same memory location. The same meaning (type), 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 5 * 4 char array example:

  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 description of an example drawing.

  • Figure A shows a full 2-dimensional array starting at address 201, and
  • &A gives the address of a full 2-dimensional array = 201
  • *A = *(A + 0) = A[0] points to the first line = 201
  • Note value A[0][0] is 'f' in my example, and &A[0][0] is the address [0][0] element = 201
  • Note &A[0][0] matches *A because &A[0][0] => &(*(*A)) => &**A => *A

Thus, all A[0] , &A , *A , A same, but symmetrically distinct.

Observe the difference between A[0] , &A , *A , A Enter the text sizeof() . eg

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

A second attempt to print the following location address using:

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

For a more detailed explanation, read this answer .

+1
source share

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

& A is a little trickier. A is of type int [5] [5], which is represented by a continuous region of 100 bytes on the stack. Address A is the beginning of this area, which is equal to the pointer to the first element. This first item address is also * A.

0
source share

An array at its most basic level is a pointer to a spot in memory. The remaining elements of the array are saved sequentially after this element, and the index tells the computer how many places you need to jump from the first element to go to the desired one. A[0] prints the address of the first element in the first line, &A prints the address where A is, where the first element of the first line is, and *A matches A[0] .

0
source share

All Articles