function pointers as function parameters
- From: Marlene Stebbins <marlene@xxxxxxxx>
- Date: Mon, 02 May 2005 18:13:32 GMT
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;
}
.- Follow-Ups:
- Re: function pointers as function parameters
- From: Jonathan Bartlett
- Re: function pointers as function parameters
- From: Emmanuel Delahaye
- Re: function pointers as function parameters
- From: Eric Sosman
- Re: function pointers as function parameters
- Prev by Date: Re: Differences between one-dimensional arrays in Java and C
- Next by Date: Re: mutually referential (Pg 140 K&R2)
- Previous by thread: Checking if socket is still open?
- Next by thread: Re: function pointers as function parameters
- Index(es):
Relevant Pages
|