By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,376 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,376 IT Pros & Developers. It's quick & easy.

Print a function pointer always results in a warning

P: n/a
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

Dec 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Steven Woody" <na********@gmail.com> wrote:
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
Dec 12 '05 #2

P: n/a
"Steven Woody" <na********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
Dec 12 '05 #3

P: n/a

Richard Bos wrote:
"Steven Woody" <na********@gmail.com> wrote:
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


thanks for the resoving and teaching me of more

Dec 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.