-
Function Pointer help
Hi im trying to make a function that returns a function pointer. ´
It would work like this, when the first function is called from my main loop, the next function it calls will be the second function.
It will remind a little bit about a state pattern, with the states changing inside the functions, and the main loop not knowing what state it is.
It looks like this:
Code:
typedef void * (*fptr)(char *, char *, signed char );
void *InitCalibration(char * upperLcdTxt, char *lowerLcdTxt, signed char Button);
void *Step2Calibration(char * upperLcdTxt, char *lowerLcdTxt, signed char Button);
void *Step2Calibration(char * upperLcdTxt, char *lowerLcdTxt, signed char Button)
{
fptr fp = InitCalibration;
char* temp = "Calibrationsmode";
char* temp2 = "Press CAL 90 KG" ;
strcpy(upperLcdTxt, temp);
strcpy(lowerLcdTxt, temp2);
return &fp;
}
void *InitCalibration(char * upperLcdTxt, char *lowerLcdTxt, signed char Button)
{
fptr fp = Step2Calibration;
char* temp = "Calibrationsmode";
char* temp2 = "Press CAL 0 KG" ;
strcpy(upperLcdTxt, temp);
strcpy(lowerLcdTxt, temp2);
return &fp;
}
Then i would attempt to cast the return value to the right function pointer from my main.
main looks like this:
Code:
fptr fp = &InitCalibration;
while (1)
{
delay_ms(2000);
keyBoardVal = keyboard();
fp = (fptr) fp(convert, convert2, keyBoardVal);
};
so every time fp function pointer calls a function its a different one, but it doesnt work, what could be wrong.
The adress changing in the main loop doesnt work, fp points nowhere, and i might have misunderstood all this function pointer stuff, so please help me correct my errors
-
Maby this works for you:
Code:
typedef void* ( *fptr ) ( char*, char*, signed char );
void* InitCalibration( char* upperLcdTxt, char* lowerLcdTxt, signed char Button );
void* Step2Calibration( char* upperLcdTxt, char* lowerLcdTxt, signed char Button );
void* Step2Calibration( char* upperLcdTxt, char* lowerLcdTxt, signed char Button ) {
fptr fp = InitCalibration;
char* temp = "Calibrationsmode";
char* temp2 = "Press CAL 90 KG";
strcpy( upperLcdTxt, temp );
strcpy( lowerLcdTxt, temp2 );
return fp; /* Here is the first error. Removed the address operator (&). */
}
void* InitCalibration( char* upperLcdTxt, char* lowerLcdTxt, signed char Button ) {
fptr fp = Step2Calibration;
char* temp = "Calibrationsmode";
char* temp2 = "Press CAL 0 KG";
strcpy( upperLcdTxt, temp );
strcpy( lowerLcdTxt, temp2 );
return fp; /* Error. Removed the address operator (&). */
}
int main( ) {
fptr fp = InitCalibration; /* Second error. Removed the address operator (&). */
while ( 1 ) {
delay_ms( 2000 );
keyBoardVal = keyboard( );
fp = fp( convert, convert2, keyBoardVal );
}
return 0;
}
But why do you want to use pointer to function to accomplish this?
-
> void *InitCalibration(char * upperLcdTxt, char *lowerLcdTxt, signed char Button);
Having declared the typedef for the function pointer, you may as well use it
fptr InitCalibration(char * upperLcdTxt, char *lowerLcdTxt, signed char Button);
-
-
Fisherrandom: I wanted to do this, because in the end it would be build up like a state pattern. The individual functions would change the state of the program by the users keyboard input.
I made it work with a global variable that got assigned the new function it should point to, and the function return value was removed.
Thx for the help, :-)
-
Why did you need to use a global to achieve that?