#include <iostream>
using namespace std;
void fn(const void *l) {
// How to print the value of l. The below line is giving error
cout << "***" << *l;
}
int main() {
cout << "Hello World!";
int d = 5;
fn((char *) &d);
return 0;
}
Error:
In function ‘void fn(const void*)’:
Line 8: error: ‘const void*’ is not a pointer-to-object type
compilation terminated due to -Wfatal-errors.
Tried Casting as seen below. It didnt not help. Please provide suggestions.
#include <iostream>
using namespace std;
void fn(const void *l) {
// How to print the value of l. The below line is giving error
int *pInt = static_cast<char*>(l);
cout << *pInt;
}
int main() {
cout << "Hello World!";
int d = 5;
fn((char *) &d);
return 0;
}
In function ‘void fn(const void*)’:
Line 9: error: static_cast from type ‘const void*’ to type ‘char*’ casts away constness
compilation terminated due to -Wfatal-errors
1 Answer