In C++ name of the array without a bracket, is a pointer to the first element of the array. However, I couldn't realize how the following code works
int main()
{
int x[] = {1, 2, 3, 4};
cout << "x = " << x << endl;
cout << "&x = " << &x << endl;
cout << "*(&x) = " << *(&x) << endl;
cout << "*x = " << *x << endl;
cout << "**(&x) = " << **(&x) << endl;
}
and this is the output
x = 0x7ffd179e9060
&x = 0x7ffd179e9060
*(&x) = 0x7ffd179e9060
*x = 1
**(&x) = 1
What I don't understand is
- why does
x = &x = 0x7ffd179e9060? - if
x = &x = 0x7ffd179e9060, then must be*x = *(&x)while*x=1and*(&x) = 0x7ffd179e9060 - Why pointer to pointer
**(&x) = 1is working here?
Thanks in advance.
Question Why are &array and array pointing to the same address?is not an answer to my question.
xand&xare not the same, they are both pointers, pointing at the same address (which is why they print the same value) but the first is a pointer to the first element, and the second is a pointer to the array. In other words they are both pointers but the type is different,*xand*(&x)are different, and why**(&x)works.