This is because the pointer to char
has its own overload of <<
, which interprets the pointer as a C string.
You can fix your code by adding a cast to void*
, which is the overload that prints a pointer:
char p;
cout << (void*)&p << endl;
Demo 1.
Note that the problem happens for char
pointer, but not for other kinds of pointers. Say, if you use int
instead of char
in your declaration, your code would work without a cast:
int p;
cout << &p << endl;
Demo 2.