-1

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 somebody explain why this is the case?

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
pcc
  • 81
  • 1
  • 1
  • 8
  • 4
    What are you expecting it to print? – Colin D Jul 12 '13 at 20:33
  • 2
    This has been asked before. `A[0]` is the first element, which decays into a pointer to be printed, `&A` is the address, which does the same and is the same address, and `*A` does the same as the first, really. – chris Jul 12 '13 at 20:34
  • So A[0] shows the address of A[0][0] ? – huseyin tugrul buyukisik Jul 12 '13 at 20:36
  • 4
    [It doesn't print the same value in *all* compilers.](http://codepad.org/RXMuphfp) Perhaps that's the more interesting question here? – Cody Gray - on strike Jul 12 '13 at 20:38
  • @chris if its a duplicate maybe it should be closed as one – Borgleader Jul 12 '13 at 20:39
  • The original title asserted an equivalence "A[0]=&A=*A". That is a different question from what might be printed from each—and the assertion is not true. – Tom Blodget Jul 12 '13 at 20:46
  • @CodyGray: It does print the same value in all strictly compliant compilers... The standard does not provide overloads for `operator<<` to print arrays of types other than `char` and user code cannot overload operators that don't operate on at least one user defined type. My guess is that the library that codepad is using has been hacked in a way that makes all programs compiled there *ill-formed*.... – David Rodríguez - dribeas Jul 12 '13 at 20:46
  • @Borgleader, I know I've seen it. Finding it is a whole new story :p – chris Jul 12 '13 at 21:09
  • @CodyGray, is C++ allowed to print the whole array when given `cout << A[0]"`?! And with that specific format? – Shahbaz Oct 03 '13 at 11:58

4 Answers4

6

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.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
1

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.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
0

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.

Norwæ
  • 1,575
  • 8
  • 18
  • 4
    How do you know it's 100 bytes? –  Jul 12 '13 at 20:40
  • C'mon... not standard but `sizeof(int) == 4` in all non-DSP, non-embedded platforms in existence. A bit more useful that those comments would be something on the lines: *You are assuming an architecture where `sizeof(int)==4`*. BTW, Norwæ, you are assuming 32bit ints! – David Rodríguez - dribeas Jul 12 '13 at 20:50
0

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].

taylorc93
  • 3,676
  • 2
  • 20
  • 34