Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C language - pointers to functions

Status
Not open for further replies.

impakt

Member level 4
Joined
Dec 19, 2005
Messages
68
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Activity points
1,799
Hello all,
I have a problem with understanding how pointers to functions work. I have some books at home, but there is poor explanation on this. Is there an article/book/link that explains how pointers to function work, but explained for dummies? Do you know something like this?
Thank you!
 

impakt said:
Hello all,
I have a problem with understanding how pointers to functions work. I have some books at home, but there is poor explanation on this. Is there an article/book/link that explains how pointers to function work, but explained for dummies? Do you know something like this?
Thank you!

Nobody? :cry:
 

They are not that difficult. A function pointer just contains the address of the entry point of the function.
If you have a function like:

int myfunction(int x);

you declare a function pointer that matches the arguments:

int (*myfunctionpointer)(int x);
The brackets define it as a function pointer.

Then you can assign the entry address of your function to it.

myfunctionpointer = myfunction; /* one way */
myfunctionpointer = &myfunction; /* Another way */

Then you call it like you would call the function:

answer = myfunctionpointer(argument_x);

There are many places were they help in writing elegant afficient code.
A simple example is in a menu system where the selection is an index into an array of functionpointers.

Here is a small example of a switch statement implemented as an index into an array of state function pointers. The return from the called function is the state switch.


Code:
/*--- State Machine ---*/

enum STATE{IDLE_S = 0, MASSAGE_S, LUMBAR_S, BOLSTER_S, ERROR_S};

/*--- State machine functions. ---*/

enum STATE idle(void);
enum STATE massage(void);
enum STATE lumbar(void);	
enum STATE bolster(void);
enum STATE error_state(void);
	
/*--- Program entry point ---*/

void main(void)
  {
  enum STATE (*const Pump_State[])(void) = {idle, massage, lumbar, bolster, error_state}; 
  enum STATE State = IDLE_S;
  
  for(;;)
    {    
    State = Pump_State[State]();  /* execute state machine */    
    }
  }
    
/*--- End of File ---*/
 

Hello btbass,
For this example do you have also the classic implementation of the switch statement? This would help me get a better view of this implementation.
Thank you!
 

The same example as a switch statement would be:

Code:
/*--- State Machine ---*/ 

enum STATE{IDLE_S = 0, MASSAGE_S, LUMBAR_S, BOLSTER_S, ERROR_S}; 

/*--- State machine functions. ---*/ 

enum STATE idle(void); 
enum STATE massage(void); 
enum STATE lumbar(void);    
enum STATE bolster(void); 
enum STATE error_state(void); 
    
/*--- Program entry point ---*/ 

void main(void) 
  { 
  enum STATE State = IDLE_S; 
  
  for(;;) 
    {    
    switch(State)
        {
        case IDLE_S: State = idle();
                     break;
 
        case MASSAGE_S: State = massage();
                        break;

        case LUMBAR_S: State = lumbar();
                       break;

        case BOLSTER_S: State = bolster();
                        break;

        case ERROR_S: State = error_state();
                      break;

        default: State =  ERROR_S;
        } 
    } 
  } 
    
/*--- End of File ---*/

This is a trivial example. Once you start using them you will find they can provide a nice clean solution for a lot of programming problems you might come up against.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top