Thread: Function Pointer help

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    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

  2. #2
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    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?
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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);

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    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, :-)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why did you need to use a global to achieve that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM