0

I am printing the addresses of variables in the following code snippet.

int int1=3;
char ch1='f';
printf("address int1 %llX\n",&int1);
printf("address ch1 %llX\n",&ch1);
cout<<"address int1 "<<&int1<<endl;
cout<<"address ch1 "<<&ch1<<endl;

Here is the output:

address int1 7FFF47D7C0F0
address ch1 7FFF47D7C0E8
address int1 0x7fff47d7c0f0
address ch1 f@

My question is Why cout prints the contents of the character rather than its address in this example? However, printf displays the addresses as shown above. Furthermore, the following also displays the address correctly:

cout<<"address ch1 "<<(void*)&ch1<<endl;
Imran
  • 642
  • 6
  • 25
  • 3
    Because you are passing `std::cout` a `char*`, which is is overloaded to read as a string literal. – Thomas Russell Jul 10 '14 at 11:42
  • `cout <<` is overloaded for `const void *` as well – yizzlez Jul 10 '14 at 11:43
  • you tell printf what to do exactly. Cout or the runtime gets the operator while the operator << is called it does not understand you want to print a pointer adress. Printing the adress of a void* is the only thing that makes sense, thats why it works. – clambake Jul 10 '14 at 11:43

0 Answers0