Print a function pointer always results in a warning

S

Steven Woody

Hi,

i am using gcc. in the following code

void ( * on_data_receive_of_ph ) ( const short line, const uint8_t c );
....
printf( "addr of function = %x\n", (void*)on_data_receive_of_ph )
--- (*)
....

the line of (*) always causes a compiler warning:
warning: unsigned int format, pointer arg (arg 2)

could anyone please help me out? thanks.

-
woody
 
R

Richard Bos

Steven Woody said:
void ( * on_data_receive_of_ph ) ( const short line, const uint8_t c );
...
printf( "addr of function = %x\n", (void*)on_data_receive_of_ph )
--- (*)
the line of (*) always causes a compiler warning:
warning: unsigned int format, pointer arg (arg 2)

That's because, surprise, you have used a format (%x) which expects you
to pass an unsigned int, but what you actually pass is a pointer. If you
want to print a void *, use %p instead.

Note that even then your code will still cause undefined behaviour. You
can only convert pointers to object or incomplete types to void *, not
function pointers. The behaviour of the cast (void *)<function pointer>
is not defined by ISO C. If it happens to work on your compiler, fine
and good; but it's not portable, and may not be reliable.

Richard
 
A

Alex Fraser

Steven Woody said:
i am using gcc. in the following code

void ( * on_data_receive_of_ph ) ( const short line, const uint8_t c );
...
printf( "addr of function = %x\n", (void*)on_data_receive_of_ph )
--- (*)
...

the line of (*) always causes a compiler warning:
warning: unsigned int format, pointer arg (arg 2)

could anyone please help me out? thanks.

The compiler is telling you that the format specifier (%x) requires an
unsigned int, but the corresponding argument has a pointer type. You should
use the %p format specifier to convert a (void *) into something
human-readable, although the precise format is implementation defined.

Note that the behaviour of converting a function pointer to (void *) is
either implementation defined or undefined (I'm not sure which), but will
usually do what you want at least on systems with a single address space for
code and data.

Alex
 
S

Steven Woody

Richard said:
That's because, surprise, you have used a format (%x) which expects you
to pass an unsigned int, but what you actually pass is a pointer. If you
want to print a void *, use %p instead.

Note that even then your code will still cause undefined behaviour. You
can only convert pointers to object or incomplete types to void *, not
function pointers. The behaviour of the cast (void *)<function pointer>
is not defined by ISO C. If it happens to work on your compiler, fine
and good; but it's not portable, and may not be reliable.

Richard

thanks for the resoving and teaching me of more
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top