home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / arptr2fn.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  1.3 KB  |  51 lines

  1. /* ARPTR2FN.CPP: Defining and Using Pointers to Functions
  2.  
  3.         This program defines a five element array of pointers to
  4.         functions that take no arguments and return nothing.  It then
  5.         initilizes the first two elements of the array with pointers
  6.         to actual functions and executes these functions via the array
  7.         pointers.
  8. */
  9.  
  10. #include <stdio.h>        // for printf()
  11.  
  12. // Define 'p' is an array of 5 pointers to functions which
  13. // take no parameters and return nothing.
  14. void (*p[5])(void);
  15.  
  16. /*-------------------------------------------------------------------
  17.     function1 - The first demo function
  18. */
  19.  
  20. void function1(void) {
  21.     printf("Hello, in function function1\n");
  22. }
  23.  
  24. /*-------------------------------------------------------------------
  25.     function2 - The second demo function
  26. */
  27.  
  28. void function2(void) {
  29.     printf("Hello, in function function2\n");
  30. }
  31.  
  32. //*******************************************************************
  33. int main()
  34. {
  35.     // Pointer at array index 0 points at function function1.
  36.     p[0]=function1;
  37.  
  38.     // Pointer at array index 1 points at function function2.
  39.     p[1]=function2;
  40.  
  41.     // Call function2 directly and then via the array
  42.     function2();
  43.     p[1]();
  44.  
  45.     // Call function1 directly and then via the array
  46.     function1();
  47.     p[0]();
  48.  
  49.     return 0;
  50. } // end of main()
  51.