reference to an element of char array

Hello. I'm learning about pointers. I'm wondering why reference to an element of char array doesn't give me address of this element.
For example:
1
2
3
4
5
6
char arr[]={ 'a', 'b', 'c', '\0' };
cout << &arr << endl;
cout << &arr[0] << endl;
cout << &arr[1] << endl;
cout << &arr[2] << endl;
cout << &arr[3] << endl;

It's output:

0x7ffff4ba8c30
abc  // should be 0x7ffff4ba8c30 ?
bc
c



It works with array of integers.

Last edited on
There is an overload for operator<<() that takes a const char * as its right hand operand and sends to the output stream the string that the pointer points to. If that overload was defined differently, you wouldn't be able to do this:
 
std::cout <<"Hello, World!\n"
You'd just get the address of the string.

To treat the pointer as a pointer, cast to void *:
cout << (void *)&arr[0] << endl;
Firstly the "&" is not reference here it is the "address of" operator

cout is treating the &arr[1] as "pointer to char" instead of pointer type

use
1
2
cout << cout << static_cast<const void*>(arr) << endl; 
cout << cout << static_cast<const void*>(arr+1) << endl; 
Ok, thanks.
Last edited on
Topic archived. No new replies allowed.