cout doesn't need dereference to access to the value of the variable pointed to by str, why?
As cire has already said:
cire wrote:
It works that way because that is the way it was designed to work.
Since traditional C-style strings are char arrays, it was decided that it would be useful to define the << operator for char* such that it outputs the string pointed to, rather than outputting the address.
@SorinAlex: that would be the address of the pointer, which isn't very useful. To print the address of the character that the pointer points to, just cast the pointer to a void pointer.
OK well aparently printf has a special %p type for pointers :
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
usingnamespace std;
#include <cstdio>
int main()
{
char* str="Hello World";
cout<<"&str --> "<< &str<<" (adrres of the pointer)"<<endl;
cout<<"(void*)str --> "<<(void*)str<<" (adrres of the data the pointer is pointing to)"<<endl;
printf("printf *str --> %p",str);
return 0;
}