Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. /*
  2.  
  3. Name:
  4. Copyright: Deitel C How to Program
  5. Author: StackOverflow1453
  6. Date: 7/5/2013 11:43:43 AM
  7. Description: Using Function Pointers to Create a Menu-Driven System
  8.  
  9. OUTPUT:
  10.  
  11. Enter a number between 0 and 2, 3 to end: 0
  12. You entered 0 so function1 was called
  13.  
  14. Enter a number between 0 and 2, 3 to end: 1
  15. You entered 1 so function2 was called
  16.  
  17. Enter a number between 0 and 2, 3 to end: 2
  18. You entered 2 so function3 was called
  19.  
  20. Enter a number between 0 and 2, 3 to end: 3
  21. Program execution completed.
  22.  
  23. */
  24. #include <stdio.h>
  25. void function1(int m);
  26. void function2(int n);
  27. void function3(int k);
  28. void function4(int p);
  29.  
  30. int main(void){
  31.  
  32.     int choice;
  33.  
  34.     /*
  35.     *fPtr is an array of 4 pointers to function which takes an integer as a parameter
  36.     *and does not return anything
  37.     */
  38.     void (*fPtr[4])(int z)={function1, function2, function3, function4};
  39.  
  40.     printf("0- Function1\n");
  41.     printf("1- Function2\n");
  42.     printf("2- Function3\n");
  43.  
  44.     printf("Enter a number between 0 and 2, 3 to end:");
  45.     scanf("%d",&choice);
  46.  
  47.     while (choice!=20)
  48.     {
  49.         /*
  50.         * choice is passed to function as array subscript and also the parameter passed to function via function pointer fPtr.
  51.         */
  52.         fPtr[choice](choice);
  53.         printf("Enter a number between 0 and 2, 3 to end:");
  54.         scanf("%d",&choice);
  55.  
  56.     }
  57.     getch();
  58. }
  59. //prints sthg
  60. void function1(int m){
  61.  
  62.     printf("You entered %d so function1 called.\n", m);
  63.  
  64. }
  65. //prints sthg
  66. void function2(int n){
  67.  
  68.     printf("You entered %d so function2 called.\n", n);
  69.  
  70. }
  71. //prints sthg
  72. void function3(int k){
  73.  
  74.     printf("You entered %d so function3 called.\n", k);
  75.  
  76. }
  77.  
  78. //function 4 exits program
  79. void function4(int p){
  80.  
  81.     printf("You entered 3 so program terminated.\n");
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement