printing of address

Can anyone explain why the following simple code doesnt print the address of a character variable in C++, while the address of the integer is printed in the same fashion. Instead of printing the address in p, the program prints the value at address p, i.e. 'h'. Strange???



int main(){
      char ch='h',*p;
      int a=100,*u;
      cout<<"\nValue of character : "<<*p<<"\nAddress of character(??) :
      cout<<"\nValue of integer : "<<*u<<"\nAddress of integer :"<<u;
      return 0;

thanking in advance,
Avik DasguptaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Because iostream treats an array of char as a string.
Not so strange.  99.9% of the time you want to print the string at "p", so the << operator assumes that.

If you look at the operators << available for iostream, you'll see the following:
ostream& operator <<( char ch );

ostream& operator <<( unsigned char uch );

ostream& operator <<( signed char sch );

ostream& operator <<( const char* psz );

ostream& operator <<( const unsigned char* pusz );

ostream& operator <<( const signed char* pssz );

ostream& operator <<( short s );

ostream& operator <<( unsigned short us );

ostream& operator <<( int n );

ostream& operator <<( unsigned int un );

ostream& operator <<( long l );

ostream& operator <<( unsigned long ul );

ostream& operator <<( float f );

ostream& operator <<( double d );

ostream& operator <<( long double ld ); (16-bit only)

ostream& operator <<( const void* pv );

ostream& operator <<( streambuf* psb );

ostream& operator <<( ostream& (*fcn)(ostream&) );

ostream& operator <<( ios& (*fcn)(ios&) );

As you can see, there's an operator <<( const char* psz );
But there is no operator <<( const int* psz );
That means it will handle int* type as an integer, and there for take the address of the integer.
Where as the string gets handle by the operator <<( const char* psz) which will display the contents of the string instead of the address of the contents.
Craig WardmanSoftware Developer & Solution ArchitectCommented:
if you need to display the address of the character, then you would need to store it first, then output the result,


int main(){
     char ch='h';
     int a=100,*u;
     long p=reinterpret_cast <long>(&ch);

     cout<<"\nValue of character : "<<ch<<"\nAddress of character : 0x"<<hex<<p;
     cout<<"\nValue of integer : "<<*u<<"\nAddress of integer :"<<u;
     return 0;

or if u still need p to be a char* then you can use a new variable, say chAddr:

int main(){
     char ch='h',*p;
     int a=100,*u;

    long chAddr=reinterpret_cast <long>(p);

     cout<<"\nValue of character : "<<*p<<"\nAddress of character : 0x"<<hex<<chAddr;
     cout<<"\nValue of integer : "<<*u<<"\nAddress of integer :"<<u;
     return 0;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.