Re: function pointers as function parameters





Marlene Stebbins wrote:
> 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?

It might be simpler to say that the function name "is"
a pointer to the function, since there is no context in
which it does anything other than "decay."

Your code is correct (as far as I can see), but there
are a few opportunities for stylistic improvement:

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

You know more about the pointed-to function than the
type of its returned value: specifically, you know that
it takes no arguments. (How do you know this? Because
you supply no arguments when you call it.) On the general
principle that it's best to tell the compiler everything
you know, write the declaration as

void f(int (*fptr)(void));

This way, the compiler will protest if you inadvertently
try to call the pointed-to function with arguments. If
you do in fact want to pass arguments, say so:

void f2(int (*fptr)(double, int, const char*));

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

Here's where ff "decays."

> return 0;
> }
>
> void f(int (*fptr)())

If you've provided a prototype as suggested above,
you should also do so here.

> {
> int a;
> a = (*fptr)(); /* deref the func pointer */

This can also be written `a = fptr();' without the
parentheses and the asterisk. Some people prefer to write
the call as you've done it, saying that it draws attention
to the fact that a function pointer variable (rather than
a function identifier) is being used. I personally don't
buy that argument, noting that

(*printf)("Hello, world!\n");

is equally legitimate and (IMHO) equally silly. However,
de gustibus non disputandum est (Latin for "There's just
no arguing with Gus").

> printf("%d\n", a);
> return;
> }
>
> int ff(void)
> {
> return 2345;
> }

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: forwards declarations!
    ... h, long m, int w, int l); ... compiler obviously doesn't. ... LRESULT callerFunction(HWND, long, WPARAM, LPARAM), HWND ... Not quite the same as straight forwards function pointer usage. ...
    (microsoft.public.vc.language)
  • Re: Common Problems that Compilers Dont Catch
    ... where the compiler offered no help. ... Become more familiar with pointer variables and you won't be making such ... int i = p; ... Any pointer type may be converted to an integer type. ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... Why do you lie to the compiler? ... you define later takes an argument, a pointer to pointer to ... Under both Windows and Linux mainalways returns an int (and ... not know about the return type of malloc(). ...
    (comp.lang.c)
  • Re: Confused with malloc
    ... malloc without prototyppe gets ... interpreted as int malland this gets you code like: ... Not every compiler uses one and the same location with exactly the ... same number of bits for int and pointer to something. ...
    (comp.lang.c)
  • Re: Newbie question
    ... mainis a function that returns an int. ... can correct that by passing a pointer to the structure to the func- ... tion and operate on the structure belonging to the caller via this ... Some of the problems the compiler can tell you about if you invoke it ...
    (comp.lang.c)