Function pointers

R

riddhi.mittal

what is the difference in :

typedef void (fn) ();

and

typedef void (*fn) ();

the first one is used by my course reader for passing callback
functions as arguments to other functions.
the second one is used in a command dispatch table which is basically
a map with the strings as keys(names of functions) and function
pointers as values.
 
P

Pete Becker

what is the difference in :

typedef void (fn) ();

and

typedef void (*fn) ();

the first one is used by my course reader for passing callback
functions as arguments to other functions.

No, it's not. Those other functions don't take this type, they take
pointers to this type.
the second one is used in a command dispatch table which is basically
a map with the strings as keys(names of functions) and function
pointers as values.

Yup. The first is a function type and the second is a pointer to that
same function type. It's exactly the same as the difference between int
and int* Well, almost; a function type can't be used in as many places
as an int.
 
J

James Kanze

No, it's not. Those other functions don't take this type, they take
pointers to this type.
Yup. The first is a function type and the second is a pointer to that
same function type. It's exactly the same as the difference between int
and int* Well, almost; a function type can't be used in as many places
as an int.

What's probably confusing him is the fact that when used to
declare a parameter to a function, a function type is remapped
to a pointer to function type, much in the same way an array is
remapped to a pointer. So if he has something like:

void registerCallback( fn ) ;

, this is implicitly converted to:

void registerCallback( fn* ) ;

if the first typedef is used.

It's one of those obfuscation features C++ inherited from C.
 

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,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top