function pointers as function parameters



I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to a pointer?


#include <stdio.h> /* declares a function which takes an argument that is a pointer to a function returning an int */ void f(int (*fptr)()); /* function returning an int */ int ff(void);

int main(void)
{
    f(ff); /* pass the address of ff to f */

    return 0;
}

void f(int (*fptr)())
{
    int a;
    a = (*fptr)(); /* deref the func pointer */
    printf("%d\n", a);
    return;
}

int ff(void)
{
    return 2345;
}
.



Relevant Pages

  • Re: location of stack and heap varies -- why?
    ... int main{ ... C doesn't provide a way to print function pointers, ... were the same from execution to execution. ... different addresses for the global variables, ...
    (comp.unix.programmer)
  • Re: one function (in a library) - two (or more) names
    ... |>> Since function pointers can be used just like functions, ... And the compiler needs to know it is a pointer to a function, ... int; ... ldr r3, .L2 ...
    (comp.unix.programmer)
  • Re: Function Pointers
    ... reassign the function name to another block of instructions, ... I want to make sure I am getting the syntax right, ... Well, one practical use of function pointers is, as Ben pointed out, its use as a callback functions. ... int rts; ...
    (comp.lang.c)
  • Re: C aptitude
    ... pointer to a 3 element array of function pointers with a return value ... of int and a single parameter of a pointer to char ... If there ever was a good reason I would not do it like that since it can be made far clearer by using a typedef for the function type. ... void func(int (*y),int* x) ...
    (comp.lang.c)
  • Re: function pointers
    ... > Can anyone tell me the advantages of using function pointers? ... //menu items DJGPP snippet ... int bogus1; ...
    (comp.lang.c)