Home >Backend Development >C++ >How Can I Print Function Pointers Using `cout` in C ?
Printing Function Pointers with cout
In C , cout does not directly support printing function pointers. However, using a specific operator overload allows it to be done by converting the function pointer to a void pointer (void*).
The operator<< is overloaded to work with void pointers as follows:
ostream & operator <<( ostream &, const void * );
This overload allows the output of void pointers in hexadecimal format. Function pointers cannot have a standard library overload for operator<< because they exist in an infinite number of types.
When attempting to print a function pointer directly with cout, it is implicitly converted to another type. In many cases, this conversion results in a boolean value, but the exact conversion rules are implementation-defined.
To print a function pointer with cout, the following steps can be taken:
int (*pf)(); pf = foo;
cout << "cout << (void *)pf is " << (void *)pf << endl;
cout << "cout << (void *)pf is " << (void *)pf << endl;
The above is the detailed content of How Can I Print Function Pointers Using `cout` in C ?. For more information, please follow other related articles on the PHP Chinese website!