A
A
Artem2022-01-03 11:33:47
C++ / C#
Artem, 2022-01-03 11:33:47

Why are arr and &arr displayed the same way?

The code:

int arr[] = {1, 2};
cout << arr << '\n';
cout << &arr<< '\n';
cout << *arr << '\n';
cout << *&arr << '\n';

Conclusion:
0x7ffec51bafa0
0x7ffec51bafa0
1
0x7ffec51bafa0

From the first two outputs, it seems to me that arr is a pointer pointing to itself

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-01-03
@Trame2771

Arrays and pointers in C are not exactly the same thing. Perhaps I'll just give a link to SO (there is even an excerpt from the standard).
Roughly speaking, variables of type "array" do not always behave identically to variables of type "pointer to the first element of the array". In particular, in the well-known construction:

int arr[32] = {...};
size_t N = sizeof(arr) / sizeof(arr[0]);

And in your case with the operator &, there is also an exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question