int and char pointer difference

Dec 3, 2011 at 8:03am
Well, sorry if it sounds too naive, but I am not able to understand why value stored in a char pointer is not address itself, like integer pointers. Eg. I am trying following code.

----------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int arr[5]={1,2,3,4,5};
int* p;
p=arr;

cout << "value pointed by p is\n"<<*p<<endl; //1
cout << "value stored in p is\n"<<p<<endl; //0x22cd08

return 0;
}
----------------------------------------------

on the other hand, if I try the same program with char* I get following result
--------------------------------------------------------------------
#include<iostream>
using namespace std;

int main()
{
char c[6]="Hello";
char* p;
p=c;

cout << "value pointed by pointer is\n"<<*p<<endl; //H
cout << "value stored in pointer is\n"<<p<<endl; //Hello -why not the address?
return 0;
}
-------------------------------------------------------------------
Could anybody please explain? :(
P.S. I am using cygwin.
Dec 3, 2011 at 8:57am
char* does store the address, but cout << char* doesn't print it, because char* is a string. There's a thing called function overloading, meaning, that with different arguments functions (or operators) may behave differently.
If you want the address, cout << (void*)p;
Dec 3, 2011 at 10:06am
Thanks for the help :D It does print it now ! But still not clear.
//char* does store the address, but cout << char* doesn't print it, because char* is a string. Isn't char* simply a pointer(like any other pointer type) that points to a character type? Why do we consider it as a string?
Dec 3, 2011 at 10:20am
What @hamsterman means is that even though both are pointers (int* and char*) different function handle them. i.e.
ostring operator<<(int*);
ostring operator<<(char*); (I don't remember exact prototypes and these ones might be wrong...)

compiler chooses the correct one and so you have (sometimes) different behaviour. This normally is considered OK and desirable but in this case it seemed confusing for you.
Dec 3, 2011 at 11:22am
Hmm..Makes sense :) Thanks for the help. Got a pretty clear idea now ;)
Topic archived. No new replies allowed.