Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
If we declare:
int i;
int *ptr1 = &i;
*ptr1=10;
cout << ptr1;

Here ptr1 will give the address.

But:
char *ptr2;
ptr2="Priyaa";
cout << ptr2;

Here it will give the content of the character pointer.

Why is there such a difference?
Posted
Updated 3-Feb-10 22:09pm
v5

Because a char * is the only way to define a string in C, and is commonly used for that purpose in C++. So, it's designed to deal with a char * in the manner that it expects you'd want to deal with it.
 
Share this answer
 
cout << (int *) and cout << (char *)
call different methods of the ostream::operator << because they have to handle different parameter types.
There are several overloaded methods of the operator <<. If you want to have a different behaviour, overload the operater << yourself for the types you want to change.
 
Share this answer
 
v5
The difference is in the implementations of the two overloads of the ostream operator <<.
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900