#include <iostream>
int main()
{
char *p = new char[1024];
std::cin >> p;
char q = *p;
std::cout << "&q = " << (void*)&q << '\n';
return 0;
}
My question is what is the meaning of (void*) in this case and why does it output the address of q but if this snippet looked like this:
std::cout << "&q = " << &q << '\n';
it outputs the character inputted and gibberish after like so: aó√ä²$↔4 where I entered a as the char. Does the usage of (void*) only apply to characters or would I have to use this when I want to output the address of q lets say but its an int or a string.
Thanks
CodePudding user response:
Insertion operator (<<) for std::ostream objects (of which std::cout is one of the examples) has a specific overload for char*/const char* pointers, where it treats them as C-style null-terminated strings (of which &q obviously is not).
For all other pointers (including a void* one), a different overload is used, the one which just prints the pointer's value.
