I have just started learning C++ and am playing with pointers. I think I understand the concepts, but whilst playing with pointers just now I have a question.
In the following code, I don't understand why the memory address for myChar is showing the same as the value itself. Why is it not giving me a memory location such as 0x123456 ?
when the << operator sees a char* is assumes you are working with a char array
because when you pass a char array to a function it decays to a pointer. you can try casting the char* to a different type of pointer such as void*.
Wow.. I wouldn't ever have guessed that's why it wasn't working!
So by casting the char* pointer into a generic void pointer, the << operator kind of 'gives up', and no longer assumes you've given it a character array, and thus does what I expected it to. I see... So the << operator is just a part of the C++ language designed to work like that?
Thanks for the information.. I shall try to remember it..
Its just because the operator << is overloaded so that when you have something like char intro[14] = "Hello, World!"; you can output it like: std::cout << intro << std::endl; instead of getting a memory address and having to do something like:
1 2 3 4 5 6 7
for( int i = 0; i < SIZE; ++i )
std::cout << intro[i];
std::cout << std::endl;for( int i = 0; intro[i]; ++i )
std::cout << intro[i];
std::cout << std::endl;