0
what is difference between char* p="122"; and int* i=122; why cout<<p; prints 122 whereas cout<<i; prints address.
4 Antworten
+ 7
int* p is a pointer to an 'int'.
char* p is a pointer to a 'char'.
+ 2
edits question.. after answers...
+ 2
@Testing002 Theres no need for a cast, You'd just do std::cout << &str;
+ 1
When couting a char pointer, cout will not treat that as a memory address, but as a C array.
So if you have
char* str = "abc";
cout << str;
it will print the values abc.
If you want to get the address of str, you could cast it to another pointer data type, like so:
cout << (int*)str;