what is difference between char* p="122"; and int* i=122; why cout<<p; prints 122 whereas cout<<i; prints address. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is difference between char* p="122"; and int* i=122; why cout<<p; prints 122 whereas cout<<i; prints address.

22nd May 2017, 2:05 PM
LAKSHMI NARAYANA SANTHA
4 Answers
+ 7
int* p is a pointer to an 'int'. char* p is a pointer to a 'char'.
22nd May 2017, 2:18 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 2
edits question.. after answers...
22nd May 2017, 2:32 PM
jay
jay - avatar
+ 2
@Testing002 Theres no need for a cast, You'd just do std::cout << &str;
22nd May 2017, 4:19 PM
aklex
aklex - avatar
+ 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;
22nd May 2017, 3:36 PM
Testing002