0

I am fairly new to C++ and am trying to understand pointers. Why does the following code output asdf instead of the memory address of a? From my understanding, a pointer is a variable that stores a memory address of type, in this case, char. So wouldnt printing p simply print the memory address of the first character?

int main()
{
    char *p="asdf";
    cout<<p;
    return 0;
}
J Lem
  • 47
  • 9
  • 5
    There's [an overload of `operator<<`](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2) taking `const char*` and interpreting it as pointing to a NUL-terminated C-style string (because it's a common thing to want to do). There's [another overload](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt) taking `void*` and printing the address. Therefore, if it's the address you want, just do `cout << static_cast(p);` – Igor Tandetnik Nov 10 '17 at 02:35

0 Answers0