"ramu" <ra******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Can anyone tell me the advantages of using function pointers?
Thanks in advance.
Yes, there are a number of typical uses. A common one would be for the
action to be taken when a menu item is selected. Your code has a array of
generic menu structures each of which has an action associated with the menu
item. You can store a function pointer in the array and then use the same
code to call the appropriate function for that menu item. Another one is
when you have an address for a function outside your application space and
need to call the function. This is useful for calling the BIOS reset in RM
DOS. The ability to do this is environment specific. Another one, similar
to the menu action, would be a table of addresses of ISRs, perhaps to be
used to setup a bunch of GDT/IDT selectors. Typically, internal operating
system filesystem structures use pointers to the various file routines.
This allows a single structure to support different filesystems using
different functions with the same structure (similar to menu's).
//Boot OW1.3 code
void (__far *reset)();
reset=MK_FP(0xFFFF,0x0000);
(*reset)(); /* call the function */
//ISR's DJGPP snippet
typedef void(*isr_func)(void);
isr_func isrs[]={
isr00,isr01,isr02, /*etc... */, isrff
};
// use isrs[x] as address for GDT/IDT setup
//menu items DJGPP snippet
typedef struct {
int bogus1;
int bogus2;
int (* action)();
} menu_item;
typedef struct {
int name;
int x,y; // coordinates
menu_item item[32];
} menu;
menu my_menus[]={
{ 0,0, function1} // 'int *function1(void) must exist somewhere...
{ 2,2, function2} // ditto
}
(*my_menus[0].menu_item[6].action)(void); // sixth menu item on menu 0,
which is 'int *function1(void)'
Rod Pemberton