int a[2][2] = {{1, 2}, {3, 4}};
cout << a << endl;
cout << *a << endl;
cout << &a[0][0] << endl;
The output of this code is:
0x7fff3da96f40
0x7fff3da96f40
0x7fff3da96f40
However, if a
is 0x7fff3da96f40
then *a
should be the value in the address 0x7fff3da96f40
which is 1
.
But, we get the same address again.
Why is this happening?
CodePudding user response:
*a
should be the value in the address0x7fff3da96f40
It is. The key is what type the value has, and that type is int[2]
.
cout
can't print arrays directly, but it can print pointers, so your array was implicitly converted to a pointer to its first element, and printed as such.
Most uses of arrays do this conversion, including applying *
and []
to an array.
CodePudding user response:
If you try
int a[2][2] = {{1, 2}, {3, 4}};
cout << a << endl;
cout << *a << endl;
cout << &a[0][0] << endl;
cout << typeid(a).name() << endl;
cout << typeid(*a).name() << endl;
cout << typeid(&a[0][0]).name() << endl;
the possible output is
0x7ffe4169bb50
0x7ffe4169bb50
0x7ffe4169bb50
A2_A2_i
A2_i
Pi
So now you can see what types are the variables you are using. They are somewhat different entities, starting on same memory address.