Why does a pointer variable to a const char not store address ?

Why is the output "12345" and not the address at p since p is a pointer and by definition, pointer is a variable that stores the address?

1
2
  const char* p = "12345";
  cout<< p <<endl;


Last edited on
std::ostream& operator<<(std::ostream&, const char*) is overloaded to print the null-terminated string that the pointer points to.
This is for convenience to allow constructs like cout << "hello"; to be possible.

Rarely do you ever need to know the actual address of string literals, but if you insist,
do cout << reintepret_cast<const void*>(p) << endl;

1
2
3
4
5
6
7
8
// Example program
#include <iostream>

int main()
{
  const char* plit = "Hello";
  std::cout << reinterpret_cast<const void*>(plit) << std::endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
  const char* p = "Hello";
  cout << p << '\n';
  cout << reinterpret_cast<const void*>(p) << '\n';
  cout << (void *)p << '\n';
}

Thank you, @Ganado and @lastchance .
Topic archived. No new replies allowed.