Re: Take and return pointer to function



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
.



Relevant Pages

  • Re: Function pointer prototype interpretation
    ... > I came accross this FP signature and it has me baffled. ... Did you try cdecl? ... declare foo as pointer to function returning pointer to function ...
    (comp.lang.c)
  • Re: Function pointer prototype interpretation
    ... > I came accross this FP signature and it has me baffled. ... declare foo as pointer to function returning pointer to function (int, ... pointer to function returning void) returning void ...
    (comp.lang.c)
  • Re: Need to find size of destination buffer for strncpy
    ... pointer really points to an allocated buffer. ... /* This signature will be written at the start of the ... static long AllocatedMemory; ... Many systems have smaller int than size_t, and this unnecessarily restricts what can be allocated. ...
    (comp.lang.c)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)