How to print pointer to function?

Liu Hao
Wed Dec 18 02:39:00 GMT 2019

在 2019/12/18 上午12:30, Vincent Lefevre 写道:
> On 2019-12-17 16:27:28 +0100, Manfred wrote:
>> On 12/17/2019 2:22 PM, Segher Boessenkool wrote:
>>> Is there *any* portable way to print function pointers?  Other than
>>> accessing it as bytes :-)
>> n1570 section p6 says "Any pointer type may be converted to an
>> integer type. Except as previously specified, the result is
>> implementation-defined. ..."
>> It says implementation-defined, not undefined,

There's an enormous number of implementation-defined things, which
people kind of rely on to write 'portable code':

0. Conversion a value from `unsigned int` to `signed char` which doesn't
   fit in it yields an implementation-defined result.
   [C++14 now requires 2's complement, which is required by GCC.]
1. Shifting a negative integer to the right yields an impl-def result.
   Shifting a negative integer to the left is undefined behavior.
2. Calling `fflush()` on an input stream results in undefined behavior.
   [The behavior is defined by POSIX 2008.]
3. Comparing two pointers that do not point to elements or past-the-end
   position of the same array, using one of <, >, <= or >= operators,
   results in undefined behavior.
   [C++ says the result is unspecified.]

So how can you write 'portable code' without regard to these facts?

In addition, even if it could be 'portable' to have standard casts
between `void*` and function pointers, the `%p` specifier of `printf()`
is still impl-def. So how can you make such combination more 'portable'?

Best regards,

More information about the Gcc-help mailing list