Static_cast from 'const char *' to 'void *' is not allowed

In C ++, I'm trying to print the address of a C string, but there seems to be some problem with my cast. I copied the code from the book, but it just doesn't compile on my mac.

const char *const word = "hello";
cout << word << endl; // Prints "hello"
cout << static_cast< void * >(word) << endl;  // Prints address of word

      

+3


source to share


1 answer


You are trying to discard a "constant": word

points to constant data, but the result is static_cast<void*>

not a pointer to constant data. static_cast

won't let you do it.



Should be used instead static_cast<const void*>

.

+13


source







All Articles