Re: functions' pointer



"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"
.



Relevant Pages

  • Re: bug in visual studio .net 2003 - breakpoints and memcpy
    ... > from a function pointer to void*, ... > listed among the possible conversions. ... Otherwise the conversion is an error and the "double cast" doesn't ...
    (microsoft.public.win32.programmer.kernel)
  • Re: User defined stack for threads in Linux 2.6.11 + glibc 2.3.5
    ... printf, sleep(), and pthread_exitaren't async-cancel-safe, so the ... behavior is undefined if the thread running that code is actually ... The casting of 'fn' to void* is actually a constraint violation. ... structure that holds the function pointer and the value to pass as its ...
    (comp.programming.threads)
  • Re: returning function pointer
    ... function pointer some_func & meanwhile calls that function and prints ... void some_func ... int main ... Note there are two parameter lists here. ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... I have never seen an example of function pointer ... and void pointer have any interaction. ... Great programmers make ... over the C programming world and only someone of very limited experience ...
    (comp.lang.c)
  • Re: Passing a pointer to a function to the function it points to
    ... void* can't be used because it might not be able to represent a ... Any type of function pointer can portably losslessly point to any ... (Identical to is the easiest form of compatibility.) ...
    (comp.lang.c)

Loading