Structs with function pointers with arguments

Example: (complex, in principle only needed when you dive into C and are interested in pointers and void pointers)

This example has an example of making an array of objects (buttons) having as members function pointers.

These function pointers take arguments being  another member of the struct.

setting the pointers and its argument:

void setFunctionPointer_buttonView( void* myFunction, BUTTONVIEW* myButtonViewPtr, float myArgument ){
void (*foo)(float);
foo = myFunction;
((BUTTONVIEW*) myButtonViewPtr)->pushedButtonFunction = foo;
((BUTTONVIEW*) myButtonViewPtr)->argumentOfPushedButtonFunction = myArgument;

example of calling the function pointed at when clicked within the button:

void clickedWithinButton_buttonView( u8 clickPosX, u8 clickPosY, BUTTONVIEW* myButtonViewPtr ){

u8 positionX = myButtonViewPtr->posX;
u8 positionY = myButtonViewPtr->posY;
u8 _width = myButtonViewPtr->width;
u8 _height = myButtonViewPtr->height;

u8 left =   positionX;
u8 right =  positionX +  _width;
u8 up =   positionY;
u8 down =   positionY + _height;

if ( ((clickPosX > left) && (clickPosX < right)) && ((clickPosY > up) && (clickPosY < down)) ){
//playDrop() ;
void (*foo)(float);
foo = myButtonViewPtr->pushedButtonFunction;
float myANgle =  myButtonViewPtr->argumentOfPushedButtonFunction;


Leave a Reply

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

You are commenting using your 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: