0
char *m_chString="asd"; 
cout<<m_chString;//prints asd
cout<<*m_chString;//prints a


int nValue = 7;
int *pnPtr = &nValue;
cout<<*pnPtr;//prints 7
cout<<pnPtr;//prints the address of nValue

I gave two examples, in the first the pointer points to a string, and in the second, the pointer prints to an int value.
My question is, why cout<<m_chString; from the first example doesn't print the address of my string as it would do in the second example if I were to print pnPtr without dereferencing it?
Doesn't pnPtr point to an address?

Baldrickk
  • 4,093
  • 1
  • 13
  • 26
George Irimiciuc
  • 4,355
  • 5
  • 40
  • 84

1 Answers1

2

The reason for that is that std::cout will treat a char * as a pointer to (the first character of) a C-style string and print it as such.

You can print the address by:-

    cout << (void *) m_chString;

OR if you are great C++ fan then

    cout << static_cast <const void *> (m_chString);
ravi
  • 10,736
  • 1
  • 14
  • 33