473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Print a function pointer always results in a warning

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
3 8935
"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
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lou zion | last post by:
hi all, i've got a class that takes a parameterless function pointer as a parameter. i want to store that function pointer in a variable and i'm trying to figure out the syntax. i came up with...
11
by: Rajesh | last post by:
Dear All, Please let me know the advantage of function pointer? Is it fast calling function using function pointer? Is it possible to use function pointer to optimise code? Thanks and regards...
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
5
by: Giannis Papadopoulos | last post by:
I have the following code #include <stdio.h> void a(void) { printf("a called.\n"); } int b(void) { printf("b called.\n");
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
2
by: Daugaard | last post by:
As the title says, I'm trying to put a function pointer in a structure at initialization time. The code below demonstrates this and also includes code for testing whether or not it works. It's fairly...
0
by: Stephen Sprunk | last post by:
sh.vipin@gmail.com wrote: .... In the particular instances of unary & or sizeof, "gef" does _not_ decay to pointer-to-function. sizeof is only defined for objects, not functions (e.g. gef). ...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.