/*----------------------------------------------------- Script for AdSense -----------------------------------------------------*/ /* */ /* Footer ----------------------------------------------- */ #footer { clear: both; text-align: center; color: #333333; } #footer .widget { margin:.5em; padding-top: 20px; font-size: 85%; line-height: 1.5em; text-align: left; } /** Page structure tweaks for layout editor wireframe */ body#layout #header { width: 750px; } -->

Tuesday, April 20, 2010

Pointer to Function (Alternative to switch-case)

In previous post, we have seen Function pointer. Even though a function is not a variable, it still has a physical location in memory that can be assigned to a pointer. A function's address is the entry point of the function. Because of this, a function pointer can be used to call a function

For example, in an accounting program, you may be presented with a menu that has 20 or more selections.The most common way is to use a switch statement. However, in applications that demand the highest performance, there is a better way. An array of pointers can be created with each pointer in the array containing the address of a function. The selection made by the user is decoded and is used to index into the pointer array, causing the proper function to be executed. This method can be very fast--much faster than the switch method.

However, in applications that demand the highest performance, there is a better way. An array of pointers can be created with each pointer in the array containing the address of a function. The selection made by the user is decoded and is used to index into the pointer array, causing the proper function to be executed. This method can be very fast--much faster than the switch method.


void enter(void), del(void), review(void), quit(void);
int
menu(void);

void
(*options[])(void) = {
enter,
del,
review,
quit
} ;

//Notice how the menu( ) function automatically returns the proper index into the pointer array:
int main(void)
{

int
i;

i = menu(); /* get user's choice */

(*
options[i])(); /* execute it */
return
0;

}


int
menu(void)
{

char
ch;

do
{

printf("1. Enter\n");
printf("2. Delete\n");
printf("3. Review\n");
printf("4. Quit\n");
printf("Select a number: ");
ch = getche();
printf("\n");
}
while(!strchr("1234", ch));
return
ch-49; /* convert to an integer equivalent */
}


void
enter(void)
{

printf("In enter.");
}


void
del(void)
{

printf("In del.");
}


void
review(void)
{

printf("In review.");
}


void
quit(void)
{

printf("In quit.");
exit(0);
}



The program works like this. The menu is displayed, and the user enters the number of the selection desired. Since the number is in ASCII, 49 (the decimal value of 0) is subtracted from it in order to convert it into a binary integer. This value is then returned to main( ) and is used as an index to options, the array of function pointers. Next, the call to the proper function is executed.

Using arrays of function pointers is very common, not only in interpreters and compilers but also in database programs, because often these programs provide a large number of options and efficiency is important.

Source: C++ Complete Reference.

No comments: