So i was reading the learncpp tutorial, and i was trying to print the address of a char pointer, because when you do :
char c{'i'}; cout << &c << endl; It will just print the string "i", because i guess the compiler identify a char pointer to a char array, which is a string (however i don't understand why it's just printing "i", because there is not '\0' after the character 'i').
Anyway, i then tried to static_cast the char pointer into an int pointer so that is will print the address :
char c{'i'}; cout << static_cast<int*>(&c) << endl; But this doesn't even compile, i have an error "invalid static_cast from the type << char* >> to the type << int* >>".
Finally, i simply tried a C-type cast like this :
char c{'i'}; cout << (int*)(&c) << endl; And it works, it prints an address.
So my questions are :
- why the static_cast doesn't work ? Is there any way to make a valid "C++ style cast" from char* to int* ?
- in the first example, when I "cout << &c", why does it only print "i", while i haven't put any '\0' after the character 'i' ?
Thanks
reinterpret_castnot astatic_caststatic_castis not the same as a c cast ((T*)). A c cast will cast just about anything to anything else regardless of type safety.static_castonly allows some of the safer conversions.