Re: functions' pointer
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 19 Mar 2007 17:49:41 -0700
"David R Tribble" <david@xxxxxxxxxxx> writes:
gio wrote:
Can I have a definition of a pointer to a generic function ?
whitout specifing the type and number of the arguments?
Something like this can work?
void * (*fptr) (...);
[I'll ignore whether this is worthy of posting to comp.std.c]
A better approach is to use:
void (*fptr)();
which declares a function pointer with no prototype, i.e.,
with no information about the number or type of its arguments.
Why is that better?
The standard doesn't provide a generic function pointer type,
analagous to void* which acts as a generic object pointer type.
Instead, it allows explicit conversions from any function pointer type
to any other function pointer type. In effect *any* function pointer
type can be treated as generic -- but you still have to ensure that
the pointer is converted to the correct type before being used to call
the function. (Keeping track of that is a matter of programming.)
I'd either use the simplest possible type:
void (*fptr)(void)
or I'd create some otherwise unused type to guarantee uniqueness:
struct NEVER_USE_THIS_TYPE { char c; };
typedef void (*generic_function_pointer)(struct NEVER_USE_THIS_TYPE);
The latter avoids the error of calling a function through a generic
pointer without first converting it.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- functions' pointer
- From: gio
- Re: functions' pointer
- From: David R Tribble
- functions' pointer
- Prev by Date: Re: functions' pointer
- Next by Date: Re: functions' pointer
- Previous by thread: Re: functions' pointer
- Next by thread: Re: functions' pointer
- Index(es):
Relevant Pages
|
Loading