Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name:
- Copyright: Deitel C How to Program
- Author: StackOverflow1453
- Date: 7/5/2013 11:43:43 AM
- Description: Using Function Pointers to Create a Menu-Driven System
- OUTPUT:
- Enter a number between 0 and 2, 3 to end: 0
- You entered 0 so function1 was called
- Enter a number between 0 and 2, 3 to end: 1
- You entered 1 so function2 was called
- Enter a number between 0 and 2, 3 to end: 2
- You entered 2 so function3 was called
- Enter a number between 0 and 2, 3 to end: 3
- Program execution completed.
- */
- #include <stdio.h>
- void function1(int m);
- void function2(int n);
- void function3(int k);
- void function4(int p);
- int main(void){
- int choice;
- /*
- *fPtr is an array of 4 pointers to function which takes an integer as a parameter
- *and does not return anything
- */
- void (*fPtr[4])(int z)={function1, function2, function3, function4};
- printf("0- Function1\n");
- printf("1- Function2\n");
- printf("2- Function3\n");
- printf("Enter a number between 0 and 2, 3 to end:");
- scanf("%d",&choice);
- while (choice!=20)
- {
- /*
- * choice is passed to function as array subscript and also the parameter passed to function via function pointer fPtr.
- */
- fPtr[choice](choice);
- printf("Enter a number between 0 and 2, 3 to end:");
- scanf("%d",&choice);
- }
- getch();
- }
- //prints sthg
- void function1(int m){
- printf("You entered %d so function1 called.\n", m);
- }
- //prints sthg
- void function2(int n){
- printf("You entered %d so function2 called.\n", n);
- }
- //prints sthg
- void function3(int k){
- printf("You entered %d so function3 called.\n", k);
- }
- //function 4 exits program
- void function4(int p){
- printf("You entered 3 so program terminated.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement