Button struct pushed function pointer

Imagine you have s struct which implements a button. One of the members of this struct is the function which is called when the button is pushed.

This is done with the function pointer, which is a void pointer (void*).

The principle can be found on interesting webpages like: http://www.cprogramming.com/tutorial/function-pointers.html.

There you see how it is done in a simple script:

void my_int_func(int x)
    printf( "%d\n", x );

int main()
    void (*foo)(int);
    foo = &my_int_func;


But how to do this in a struct? Let's do a test:
in a header file:

typedef struct{
 void *functionPtr;

 void sayHello();
 void  initTestStructAndSayGoodbye();
 void doFiePointer_buttonstruct( FIEPOINTERTEST* mytest);
 void fireFiePointer();

in a source file:

void  sayHello(){
 printf("   -------------------------\n" );
 printf("   ---greetings: zdravstbijtje-------------\n" );
 printf("   -------------------------\n\n" );

void  initTestStructAndSayGoodbye (){

 void (*foo)();
 foo = &sayHello;//init function pointer

 FIEPOINTERTEST * myTest = malloc( sizeof(FIEPOINTERTEST));
 myTest->functionPtr = (void*)foo;

//and init test struct
//and test the we need a function to fire the function pointer:

 fireFiePointer(myTest);//this is already testing
 printf ("doswidanja\n\n");//which is goodbye in russian


//so far so good, but how to implement the fireFiePointer method?

 void fireFiePointer_buttonstruct(FIEPOINTERTEST* mytest){
 void (*foo)();//make a function pointer
 foo = mytest->functionPtr; //give it the value of the member
 foo();//do the function call

This does the job, but
what does not seem to work are things like:

calling the object not a function......

So we can make the button - struct, with the image as a member and a function to be called


3 Responses to Button struct pushed function pointer

  1. Josh Mills says:

    In C++ the fireFiePointer_buttonstruct function is throwing an error: Invalid conversion from ‘void*’ to ‘void(*)()’ [-fpermissive]

    Is there a difference between how c and c++ handle void casts?

  2. Josh Mills says:

    Ah, that makes sense. I always wondered why in c++ I had to explicitly cast malloc, but not c.

    I was trying to write up a quick input manager for the ds using function pointers. That way I could have state-driven, hot-swappable input, but I kept getting tripped up by the c/c++ void * nuances. After reading through the link you provided, I was able to figure out what I was doing wrong.

    Thanks for the tutorial, this was very helpful!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: