+ 2

why not printing address?

//basic pointer #include<iostream> using namespace std; main() { char a='a'; char *p; p=&a; cout<<p<<endl; } it printing garbage value.

26th Feb 2022, 3:37 PM
Nitish
Nitish - avatar
3 Respuestas
+ 1
The reason is due to the way that cout overloaded the << operator to handle char* pointers. It assumes that you wish to print the string that it points to, rather than the pointer. A quick fix is to cast it to another pointer type that is not re-interpreted: cout<<(void*)p<<endl; More ideas are found here: https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value *** BTW, main() should be int main() and it should return a value.
26th Feb 2022, 4:12 PM
Brian
Brian - avatar
0
cout<<&p<<endl;
26th Feb 2022, 6:24 PM
the ice
the ice - avatar