Home > Archive > C > June 2006 > Take and return pointer to function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Take and return pointer to function
|
|
| Frederick Gotham 2006-06-29, 6:56 pm |
| lovecreatesbeauty posted:
> A more complex example with function pointers is the function:
>
> #include <signal.h>
> void (*signal(int sig, void (*func)(int)))(int);
I like the hardcore method ; )
/* Hardcore method */
void (*signal_hardcore(int, void (*p)(int)))(int)
{
return p;
}
/* Dumbed-down method */
typedef void (*Ptr_FuncTakeIntReturnVoid)(int);
Ptr_FuncTakeIntReturnVoid signal_dumb( int, Ptr_FuncTakeIntReturnVoid p )
{
return p;
}
/* Both used the same way */
void Func(int) {}
int main(void)
{
signal_dumb( 0, Func );
signal_hardcore( 0, Func );
}
--
Frederick Gotham
| |
| Frederick Gotham 2006-06-29, 6:56 pm |
| 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
| |
| Kenneth Brody 2006-06-29, 6:56 pm |
| Frederick Gotham wrote:
>
> Frederick Gotham posted:
>
>
> 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".
[...]
> int (*(*RetrieveOperations(void(*pCallback)(
int)))[16])(int,int)
[...]
You forgot to mention that there are 4 modes of this calculator,
and therefore it needs to return an array of 4 pointers to these
16-element arrays.
Ready... Go!
Is it possible to declare a function which returns a struct*, with
the struct definition within the prototype?
For example, rather than the 16-element array of pointers to your
handler functions, each array element is a struct consisting of a
pointer to an int version of the function, and to a double version
of the function.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>
| |
| Frederick Gotham 2006-06-29, 6:56 pm |
| Kenneth Brody posted:
> [...]
> [...]
>
> You forgot to mention that there are 4 modes of this calculator,
> and therefore it needs to return an array of 4 pointers to these
> 16-element arrays.
Of course, how negligent of me! Please accept my sincerest apologies.
Here's the updated code as requested ; )
/* 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;
}
/* Here's the function which retrieves four modes. */
int (*(*(*RetrieveFourModes(void(*pCallback)
(int)))[4])[16])(int,int)
{
static int (*(*array[4])[16])(int,int);
*array = RetrieveOperations( Indicate );
array[1] = RetrieveOperations( Indicate );
array[2] = RetrieveOperations( Indicate );
array[3] = RetrieveOperations( Indicate );
return &array;
}
int main(void)
{
int (*(*(*p_array)[4])[16])(int,int) = RetrieveFourModes(Indicate);
/* Now let's call a few functions */
(*((*p_array)[0]))[15](1,2);
(*((*p_array)[1]))[14](7,3);
(*((*p_array)[2]))[13](55,8);
(*((*p_array)[3]))[12](1045,87);
}
--
Frederick Gotham
|
|
|
|
|