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.

Function Pointers in C

Status
Not open for further replies.

cherry

Newbie level 3
Newbie level 3
Joined
Jul 19, 2005
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,359
function pointers in c+pdf

Where and in what situations we can use function pointers of C in Embedded?

Can somebody explain ?

Thanks.....Cherry
 


    cherry

    Points: 2
    Helpful Answer Positive Rating
state machine + function pointer

Another common application is a callback function. For example, you want Windows to call your audio recorder function each time Windows receives some samples from the microphone. You give Windows a pointer to your audio recorder function.
 

state machine c array of function pointers

Function pointers are not often used in embedded C, more for PC side programming. Basically, it's a pointer to a function, and depending on which function the pointer points to, you can implement different functions to be executed in the same line of code.

One example way of using it is that you have an array of function pointers, which points to message handlers, each of a different message type which is designed to hold the same index as the message handler.

So you can call something like
Code:
fnPtr[msg->type](msg);
[/code]

Added:
One possible use of function pointers in embedded C, is a task scheduler.
 

state machine function pointers

it is a good choice to replace switch case in a state machine using function pointer,
in the case that state machine has too many switch cases. it makes program more compact.
 

function pointers array state machine

Function pointers are very useful in embedded systems. Like a streamlined switch statement. Also very useful in menu systems. Where the menu item selected is the index of a functon pointer array.

For example, each state function here returns the next state to switch to.
The controlling code for a whole program in one line!

Code:
/*--- State machine functions. ---*/

UI_16 standby(void);
UI_16 starting(void);
UI_16 running(void);

/*--- State Machine. ---*/

enum {STANDBY = 0, STARTING, RUNNING};

/*********************************************************************
* Function Name : main 
* Description   : Program entry point.
                  Initialise array of constant function pointers.
                  Initialise state machine to standby. 
                  Execute state machine. 

*********************************************************************/

SI_16 main(void)
  {
  UI_16 (*const Motor_State[])(void) = {standby, starting, running}; 
  UI_16 State = STANDBY;

     
  for(;;){
    State = Motor_State[State]();  /* call state function */
    }
  }

/*--- End of file. ---*/
 

hello
the best use of function pointer what i feel is the implimentation of FSM and as it is told erlier 2nd one is call back function.
 

Function pointers are useful when u have a generalised procedure or algo and u want to use this procedure on different types of data.

For ex., if u have a good sorting algo, and u want to use same algo on numbers, alphabetical, strings or any data governed by some sorting rule. In that case, u have to just write a different "compare" function for each data type and in the main sorting algo, u will pass the function pointer as an argument.
This way, the same algo can be used by different ppl according to their need. No modification is required in the main body of algo.
 

I have currenlty used the function pointers in my C Project, which is the menu driven editor. The purpose of theses pointers is to write an efficient , compact and fast code. My code saple is here;

int Value[] = {0x1F00,//Alt + S (^Setup)
0x1400,//Alt + T (^Trunks)
0x1200,//Alt + E (^Extensions)
0x1800,//Alt + O (^Operator)
0x2600,//Alt + L (^Logging)
0x1900,//Alt + P (^Passwords)
0x2200,//Alt + G (Settin^gs)
0x2300 //Alt + H (^Help)
};

// ------ Pointers to functions ---------------------------------
void (*Execute[8])();//Main Menu functions
void (*Setup[3])();//Menu Item functions
void (*Trunks[9])();
void (*Extensions[4])();
void (*Operator[3])();
void (*Logging[2])();
void (*Passwords[3])();
void (*Settings[6])();
void (*Help[10])();
//---------------------------------------------------------------

main()
{
....
//Initialize funtion pointers
Execute[0] = ProcessSetup;
Execute[1] = ProcessTrunks;
Execute[2] = ProcessExtensions;
Execute[3] = ProcessOperator;
Execute[4] = ProcessLogging;
Execute[5] = ProcessPasswords;
Execute[6] = ProcessSettings;
Execute[7] = ProcessHelp;
......

// Read the keyboard ...
i = bioskey (1);
if (i) // If keyboard ready ...
key = bioskey (0);
else
continue; // If keyboard not ready then continue watching it

// Execute the menu selected by the user
for ( j = 0; j < 8; j++)
{
if(key == Value[j])
{
if(!inProcess)
{
inProcess = 1;
Execute[j]();
}
}
else if(key == 0x2D00)//Alt+X pressed...i.e. Quit
return;
}
..........
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top