Re: Take and return pointer to function
- From: Frederick Gotham <fgothamNO@xxxxxxxx>
- Date: Thu, 29 Jun 2006 18:22:15 GMT
Frederick Gotham posted:
I like the hardcore method ; )
Who's up for role play? I have an idea for a game!
You've to come up with as complicated a function signature as you can,
but... you also have to devise a story in order to justify its complexity.
OK I'm first!
How about some sort of "runtime-programmable calculator".
All of its operations can be performed using simple functions which all
have the same signature, e.g.:
int Add(int,int);
int Subtract(int,int);
int Divide(int,int);
The calculator must be supplied at runtime with 16 such functions.
The programmable calculator queries a database looking for these 16
operations, and receives a pointer to a 16-element-array of pointers to
functions. (It returns a pointer to an array of definite size rather than a
pointer to the first element of an arrary in order to emphasize that the
array's length must be 16.). Furthermore, in the same query, you must
supply the database with a callback function which it should invoke before
and after it goes checking the database, to indicate its current status.
The callback funtion should have the following signature:
void Indicate(int);
Okay here's what I've got:
/* Let's start off with an actual function */
#include <stdio.h>
int Addition(int const a, int const b)
{
int result = a + b;
printf( "The addition of %i and %i yields: %i\n", a, b, result );
return result;
}
/* Here's the callback function which gets invoked to
inform us of the database's current status. */
void Indicate(int) {}
/* Here's the function which accesses the database and retrieves
our mathematical operations as pointers to functions. */
int (*(*RetrieveOperations(void(*pCallback)(int)))[16])(int,int)
{
pCallback(0);
static int (*array[16])(int,int) =
{ Addition, Addition, Addition, Addition, Addition, Addition,
Addition, Addition, Addition, Addition, Addition, Addition,
Addition, Addition, Addition, Addition };
pCallback(1);
return &array;
}
int main(void)
{
int (*(*p_array)[16])(int,int) = RetrieveOperations(Indicate);
/* Now let's call a few functions */
(*p_array)[0](1,2);
(*p_array)[4](3,4);
(*p_array)[9](5,6);
(*p_array)[12](7,8);
}
Looking forward to reading others' contributions : )
--
Frederick Gotham
.
- Follow-Ups:
- Re: Take and return pointer to function
- From: Kenneth Brody
- Re: Take and return pointer to function
- References:
- Function Pointers
- From: noridotjabi@xxxxxxxxx
- Re: Function Pointers
- From: lovecreatesbeauty
- Take and return pointer to function
- From: Frederick Gotham
- Function Pointers
- Prev by Date: Re: About header files
- Next by Date: Re: Problems with typedef
- Previous by thread: Take and return pointer to function
- Next by thread: Re: Take and return pointer to function
- Index(es):
Relevant Pages
|