/* Name Description ** ------- ------------------------------------------------------- ** funcptr.c This small C program shows how program flow ** can be controlled by function pointers. ** ** This example program was brought to you by ** Scott Wenger, Box 802, Stevens Point, WI 54481, USA ** panther@wctc.net ** http://www.private-files.com/sware.html ** ** In the example below, you will see that an array ** of function pointers is declared. Allowing ** the user to move forward or backward through that ** array controls program flow. ** ** Naturally, many more functions can be used but ** this example gives you the outline. ** ** This code compiles, links, and executes properly. ** Check your specific implementation of getch, if necessary */ #include #include #include // for getch() #define NUMB_FUNCTIONS 5 // number of functions // function prototypes int f1(), f2(), f3(), f4(), f5(); void title(); void prompt(int i); void clr_scr(); void s_line(); main() { // declare func as an array of function pointers // and initilize the array with the function ptrs. static int (*func[])() = { f1, f2, f3, f4, f5 }; int retval; int active = 1; int i = -1; int ch; // Functions can be traversed, forward or backward // through an array of pointers to functions as shown below. // In this example, the user controls the direction of program flow. clr_scr(); title(); while (active) { prompt(i); ch = getch(); // get direction from user (forward or backward) if (ch == 'x' || ch == 'X') break; // Check valid range if (i >= -1 && i < NUMB_FUNCTIONS - 1 && (ch == 'f' || ch == 'F') ) i++; else if (i > 0 && i < NUMB_FUNCTIONS && (ch == 'b' || ch == 'B') ) i--; retval = ( *(func[i]) )(); // call the functions } exit(0); } int f1() { // this function does whatever you want printf("\n>>>>> i is 0, so *(func[i]))() gets us inside function #1\n\n\n"); s_line(); return(1); } int f2() { // this function does whatever you want printf("\n>>>>> i was 1, so *(func[i]))() gets us inside function #2 \n\n\n"); s_line(); return(1); } int f3() { // this function does whatever you want printf("\n>>>>> i was 2, so *(func[i]) )() gets us inside function #3 \n\n\n"); s_line(); return(1); } int f4() { // this function does whatever you want printf("\n>>>>> i was 3, so *(func[i]))() gets us inside function #4 \n\n\n"); s_line(); return(1); } int f5() { // this function does whatever you want printf("\n>>>>> i was 4, so *(func[i]) )() gets us inside function #5 \n\n\n"); s_line(); return(1); } void prompt(int i) { if (i == -1 || i == 0) printf("Press 'f' to move forward or 'x' to quit\n"); if (i == NUMB_FUNCTIONS - 1) printf("Press 'b' to move backward or 'x' to quit\n"); if (i > 0 && i < NUMB_FUNCTIONS - 1) printf("Press 'f' for forward, 'b' for backward, or 'x' to quit\n"); } void title() { printf("\n\n"); printf(" ********************************************************************\n\n"); printf(" This example (funcptr.c) uses an array of function pointers \n"); printf(" to control program flow. \n"); printf(" \n"); printf(" The array of function pointers was declared as follows: \n"); printf(" static int (*func[])() = { f1, f2, f3, f4, f5 }; \n"); printf(" \n"); printf(" This example uses the variable 'i' as an index into the array. \n"); printf(" Remember that, in C, arrays are 0-based. \n\n"); printf(" ********************************************************************\n"); printf("\n\n\n\n\n"); } void s_line() { register int z; for (z = 0; z < 75; z++) printf("-"); printf("\n\n"); } void clr_scr() { register int z; for (z = 0; z < 80; z++) printf("\n"); }