| Author |
Message |
cherry
Joined: 19 Jul 2005 Posts: 3
|
25 Aug 2005 6:01 function pointers in c+pdf |
|
|
|
|
Where and in what situations we can use function pointers of C in Embedded?
Can somebody explain ?
Thanks.....Cherry
|
|
| Back to top |
|
 |
IanP
Joined: 05 Oct 2004 Posts: 6490 Helped: 1542 Location: West Coast
|
25 Aug 2005 6:24 function pointers switch |
|
|
|
|
| Quote: |
| Pointers to functions are not as common as other pointer uses. However, one common use is in a passing pointers to a function as a parameter in a function call. |
http://www.cs.cf.ac.uk/Dave/C/node12.html
Regards,
IanP
|
|
| Back to top |
|
 |
echo47
Joined: 07 Apr 2002 Posts: 4206 Helped: 566
|
25 Aug 2005 7:13 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.
|
|
| Back to top |
|
 |
checkmate
Joined: 25 Feb 2004 Posts: 489 Helped: 35 Location: Toilet Seat
|
25 Aug 2005 12:23 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.
|
|
| Back to top |
|
 |
Google AdSense

|
25 Aug 2005 12:23 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
njfane
Joined: 27 Oct 2004 Posts: 15
|
26 Aug 2005 10:00 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.
|
|
| Back to top |
|
 |
Bingo600
Joined: 26 Aug 2004 Posts: 239 Helped: 14
|
27 Aug 2005 13:35 function pointers in c state machine |
|
|
|
|
This is a nice dokument on the subject
http://www.newty.de/fpt/zip/e_fpt.pdf
http://www.newty.de/fpt/index.html
/Bingo
|
|
| Back to top |
|
 |
btbass
Joined: 20 Jul 2001 Posts: 1187 Helped: 113 Location: Oberon
|
10 Sep 2005 14:21 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. ---*/
|
|
|
| Back to top |
|
 |
raj_rohit10
Joined: 14 Jul 2004 Posts: 113 Helped: 1
|
13 Sep 2005 6:19 Re: Function Pointers in C |
|
|
|
|
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.
|
|
| Back to top |
|
 |
abhishek_elec
Joined: 09 Sep 2005 Posts: 15 Helped: 1 Location: India
|
13 Sep 2005 11:42 Re: Function Pointers in C |
|
|
|
|
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.
|
|
| Back to top |
|
 |
yaseen
Joined: 22 Jan 2006 Posts: 9 Helped: 1
|
27 Aug 2006 8:33 Re: Function Pointers in C |
|
|
|
|
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;
}
..........
|
|
| Back to top |
|
 |